5/5
# Method ## Example Execute the following command to run [`./solutions/examples/method.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/method/solutions/examples/method.rs) ```shell cargo run --example method ``` ## Exercises Exercises are in [`./exercises/src/lib.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/method/exercises/src/lib.rs) ### Exercise 1 ```rust pub fn new(width: u32, height: u32) -> Self { todo!(); } ``` Return `Point` with `top` set to 0, `left` set to 0, `width` and `height` set from the inputs. ### Exercise 2 ```rust pub fn move_to(&mut self, top: u32, left: u32) { todo!(); } ``` Update the fields `top` and `left` of the `Point`. ## Test ```shell cargo test ```
Execute the following command to run ./solutions/examples/method.rs
Exercises are in ./exercises/src/lib.rs
Return Point
with top
set to 0, left
set to 0, width
and height
set from the inputs.
Update the fields top
and left
of the Point
.
A practical guide to implementing Rust Methods - Learn to define behavior for your structs using associated functions (like `new`) and instance methods (`&self`, `&mut self`). You'll apply this by creating a `Point` struct, implementing `new` for instantiation and `move_to` to update its position.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Introduction to the Rust programming language
Rust variables and functions
Scalar types, arrays, strings, enum, structs, vectors, and hash maps in Rust
Rust control flows: If / else, if let and let else, loop, match
Rust ownership, including borrow and references
Rust error handling
Rust Modules
Rust Traits
Last updated on July 11, 2025
Rust Developer
Rust Programming BasicsDuration: 6min
Duration: 18min
Duration: 47min
Duration: 15min
Duration: 19min
Duration: 8min
Duration: 12min
Duration: 46min
Duration: 14min
Course Overview
About the course
Introduction to the Rust programming language
Rust variables and functions
Scalar types, arrays, strings, enum, structs, vectors, and hash maps in Rust
Rust control flows: If / else, if let and let else, loop, match
Rust ownership, including borrow and references
Rust error handling
Rust Modules
Rust Traits
Last updated on July 11, 2025