Mutable references play an important rule in how borrowing is achieved in Rust. This chapter discusses how to implement them and their restrictions.
- [Instructor] The semantics for passing…a value to a function are similar…to assigning a value to a variable.…Passing a variable to a function…will either move or copy just like assignment.…Let's see the following course I've provided.…Go to your Resources section.…Exercise files, Resources,…and ownership-function.…Open this folder.…Since this is generated by Cargo for us,…it should have…a main.rs file, and that's where the code is.…
So, on line two, we have a string,…and one line four we are passing it…to the takes_ownership function,…which prints out the string.…Once we are back to the main function,…after this function call,…if you try to access the value of s,…it would not be possible.…Let's run this and check it out.…Use Cargo run.…So, as you can see here,…value used here after move is the error.…Now, what if we want to let a function use a value,…but not take ownership.…
Rust has a feature for this concept,…and it's called References.…Just add an ampersand sign here,…and then an ampersand…here,…and then this should work fine.…
- Installing Rust
- Data types
- Adjusting memory allocation
- Setting up race conditions
- Working with functions
- Exploring references and borrowing
- Using structs
- Method syntax
- Demonstrating recoverable errors with Result
- Working with enums
- Getting user input
Skill Level Beginner
Programming Foundations: Data Structures (2014)with Simon Allardice2h 29m Intermediate
1. Hello, Rust!
2. Basic Programming Concepts
3. Ownership in Rust
4. Data Handling and Pattern Matching in Rust
5. Error Handling
6. Sample Rust Project: The Guessing Game
Next steps1m 14s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.