Back
Close

Rust for Python Developers - Operators

Shin_O
2,280 views

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 Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants