Rust for Python Developers - Operators

Shin_O
61.3K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

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.

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