Rust Ownership, Borrowing, and Lifetimes Exercises with Solutions and Explanations
Rust Ownership, Borrowing, and Lifetimes Exercises [10 exercises with solution and Explanation]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Write a Rust function that takes ownership of a string and prints it.
2. Write a Rust function that borrows a string and prints it.
3. Write a Rust function that takes ownership of a vector and returns its length.
4. Write a Rust function that borrows a vector and returns the first element.
5. Write a Rust function that takes ownership of a tuple and returns one of its elements.
6. Write a Rust function that borrows a slice of integers and calculates their sum.
7. Write a Rust function that takes ownership of a string and returns its length.
8. Write a Rust function that borrows a string slice and returns its first character.
9. Write a Rust function that takes ownership of a vector and returns a new vector with a doubled number of elements.
10. Write a Rust function that borrows two integers and returns their sum.
Rust Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics