Module lessons (2/2)
References, Borrowing (&T / &mut T) and NLL
XOR invariant of Borrow Checker (more &T or just one &mut T), Non-Lexical Lifetimes (NLL), dereferencing *r and string slices &str.
What you'll learn in this lesson
- Pass values to functions without giving up ownership via shared
&Tand mutable&mut Treferences. - Respect the XOR invariant of the Borrow Checker (many readers
&TOR a single writer&mut T). - Take advantage of Non-Lexical Lifetimes (NLL) and
&str/&[T]slices for fast, idiomatic code.
Overview
Passing properties to every function and having it return would make the code verbose. Borrowing allows you to give temporary access to a value using a reference (&) without ever transferring ownership.
A reference &T allows you to read data from as many points as you like at once. If you need to change the original value in-place, you need a variable mut and an exclusive loan &mut T:
Sections covered in this lesson
- 11. Immutable (
&T) and mutable (&mut T) loans - 22. Borrow Checker Rules & NLL (Non-Lexical Lifetimes)
- 33. Interactive Borrow Checker Simulator (
&T,&mut T& NLL) - 44. Dereference (
*) and String Slice (&str) as parameters - 5Practical exercises
- 6Check for understanding
Unlock "References, Borrowing (&T / &mut T) and NLL" and the full Course Rust curriculum
XOR invariant of Borrow Checker (more `&T` or just one `&mut T`), Non-Lexical Lifetimes (NLL), dereferencing `*r` and string slices `&str`.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course Rust plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.