Rust for Python Developers - Operators


This article was originally published on Medium

Follow me:

Solution using an associated function

Since we learned about the associated function, let’s apply it to this problem.

Line 6–10: We create an associated function, new as we have done it before. This new function takes one parameter nums that is a vector with items of i32.

When the parameter names and the struct field names are exactly the same, we can use the field init shorthand syntax as nums instead of nums: nums.

In the main function, we call an associated function, new and pass nums as an argument. We use method syntax to call the single_number method on the ans3 instance.

Create your playground on
This playground was created on, our hands-on, knowledge-sharing platform for developers.
Go to