Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
This article was originally published on Medium
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
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
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