Ownership
Ownership in Rust is one of the core memory management concepts that ensures every value has a single owner responsible for it. This system avoids memory leaks and data races without needing a garbage collector. Ownership features include move semantics, where the ownership is transferred between variables, borrowing, and the borrow checker, by which these rules are checked at compile time to keep memory safe.
Syntax
Here’s the syntax for the ownership concept in Rust:
let variable1 = value;
let variable2 = variable1;
// 'variable1' is no longer valid and cannot be accessed.
variable1
: First owner of the value.value
: The value or data to be owned initially.variable2
: New owner ofvalue
after the ownership transfer.
Example
This example demonstrates how ownership works, ensuring that only one variable owns the data at a time:
fn main() {let s1 = String::from("Hello, Welcome to Codecademy!");println!("s1: {}", s1);let s2 = take_ownership(s1);// println!("s1: {}", s1); // Uncommenting this line would result in a compile-time error because 's1' no longer owns the value.println!("s2: {}", s2); // Prints: s2: Hello, Welcome to Codecademy!}fn take_ownership(s: String) -> String { // Function takes ownership of the passed valueprintln!("Taking ownership: {}", s); // Prints: Taking ownership: Hello, Welcome to Codecademy!s // Ownership is returned to the caller, moving it back to the caller.}
In this example, the ownership of the string s1 is moved to s2, and s1 is no longer valid. After moving, s2 holds the ownership of the string “Hello, Welcome to Codecademy!”:
The example will result in the following output:
s1: Hello, Welcome to Codecademy!Taking ownership: Hello, Welcome to Codecademy!s2: Hello, Welcome to Codecademy!
Unsafe Code Example
Rust’s ownership system prevents operations that could lead to unsafe memory access, like accessing data after ownership is moved, at compile time :
fn main() {let s1 = String::from("Hello");let s2 = take_ownership(s1);// println!("{}", s1); // Uncommenting this line would cause a compile-time error: use of moved value}fn take_ownership(s: String) -> String {s}
In this unsafe scenario, the ownership of s1
is moved to s2
, and attempting to access s1
after the move results in a compile-time error.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Rust on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Rust for Programmers
A quick primer on the fundamentals of the Rust programming language for experienced programmers.Intermediate1 hour