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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/rust/basic/ownership-borrowing-and-lifetimes.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics