Rust for Python Developers - Operators

Shin_O
15.4K views

Problem 1: Single Number

We are going to use this XOR to solve the LeetCoder problem called Single number.

In this problem, an array input has a pair of numbers except one, for example [1, 1, 5, 5, 2]. You need to find a sing number from this array and in this case the output should be 2.

More example: When the input is [2, 2, 1], the output should be 1. When an input is [4, 1, 2, 1, 2] the output should be 4.

This is a good example to use the XOR operator.

Python solution

We briefly go through the Python solution to see how the problem was solved.

Line 1: We use Python typing which is introduced from v3.5.

Line 3–4: After importing List, we create a class called Solution and method called singleNumber.

With Python type hints, we capitalize the name of the type, and set the name of the type inside the collection in brackets as seen above, num: List[int].

Line 5–8: We set a variable ans to 0. Using a for loop, we iterate the input array, nums using XOR compound assignment, ans ^= n. This will output the single number from the array.

Line 10–11: We instantiate the class Solution and call the method singleNumber.

(You can run this Python code without type notations if you are interested.)

The following is the solution for the LeetCode environment:

class Solution:
def singleNumber(self, nums: List[int]) -> int:
ans = 0
for n in nums:
ans ^= n
return ans


Rust code

Rust structs contain named fields. We use a keyword struct and set fields with its type within the curly bracket. We put methods into a impl block.

Starting code

Line 1: We suppress dead_code warning.

Line 2–4: Create a struct called Solution that takes one field numswith Vec<i32> type. (More on Vectors.)

Line 6–10: We create a method single_number in impl Solution. The single_number takes the first parameter &self (More on self .) and we just return 1 for now.

Line 12–17: In the main function, we create an instance and print 1 using the method.

It seems all working so we are going to complete the single_number method next.

Open Source Your Knowledge: become a Contributor and help others learn.