Rust Ownership

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

Ownership is one of the most important concepts in Rust, and it's something that isn't present in most other languages. The idea that a value can be owned by a particular variable is often quite difficult to understand, especially in languages where copying is implicit, but this section will review the different ideas surrounding ownership.

Syntax

  • let x: &T = ... // x is an immutable reference
  • let x: &mut T = ... // x is an exclusive, mutable reference
  • let _ = &mut foo; // borrow foo mutably (i.e., exclusively)
  • let _ = &foo; // borrow foo immutably
  • let _ = foo; // move foo (requires ownership)

Remarks

  • In much older versions of Rust (before 1.0; May 2015), an owned variable had a type starting with ~. You may see this in very old examples.


Got any Rust Question?