Skip to main content
Module 3 · Lesson 2 of 26/24 in the course~25 min
Module lessons (2/2)

References, Borrowing (&T / &mut T) and NLL

Lesson preview5 hands-on exercises readyCourse Rust · Module 3 — Ownership, Move and Borrowing

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 &T and mutable &mut T references.
  • Respect the XOR invariant of the Borrow Checker (many readers &T OR 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

  1. 11. Immutable (&T) and mutable (&mut T) loans
  2. 22. Borrow Checker Rules & NLL (Non-Lexical Lifetimes)
  3. 33. Interactive Borrow Checker Simulator (&T, &mut T & NLL)
  4. 44. Dereference (*) and String Slice (&str) as parameters
  5. 5Practical exercises
  6. 6Check for understanding
Pro Lesson · Module 3 — Ownership, Move and Borrowing

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.