What is Ownership? - The Rust Programming Language
Ownership is a discipline for ensuring the safety of Rust programs. To understand ownership, we first need to understand what makes a Rust program safe (or unsafe). Let’s start with an example. This program is safe to execute: We can make this program unsafe to execute by moving the call to read before the definition of x: Note: in this chapter, we will use many code examples that do not compile. Make sure to look for the question mark crab if you are not sure whether a program should compile or not. This second program is unsafe because read(x) expects x to have a value of type bool, but x doesn’t have a value yet. When a program like this is executed by an interpreter, then reading x before it’s defined would raise an exception such as Python’s NameError or Javascript’s ReferenceError. But exceptions come at a cost. Each time an interpreted program reads a variable, then the interpreter must check whether that variable is defined. Rust’s goal is to compile programs into efficient bin
What is Ownership? - The Rust Programming Language Keyboard shortcuts Press ← or → to navigate between chapters Press S or / to search in the book Press ? to show this help Press Esc to hide this help Auto Light Rust Coal Navy Ayu The Rust Programming Language What Is Ownership? Ownership is a discipline for ensuring the safety of Rust programs. To understand ownership, we first need to understand what makes a Rust program safe (or unsafe). Safety is the Absence of Undefined Behavior Let’s start with an example. This program is safe to execute: fn read(y: bool) { if y { println!("y is true!");
saved by
related reading
- References and Borrowing - The Rust Programming Languagerust-book.cs.brown.edu
- What is Ownership? - The Rust Programming Languagedoc.rust-lang.org
- Memory safety and ownership - Rust for C-Programmersrust-for-c-programmers.com
- References and Borrowing - The Rust Programming Languagedoc.rust-lang.org
- A half-hour to learn Rustfasterthanli.me
- Rust Language Cheat Sheetcheats.rs
- Summary - Rust for C-Programmersrust-for-c-programmers.com
- The Slice Type - The Rust Programming Languagedoc.rust-lang.org
- Moves, cloning, and copying - Rust for C-Programmersrust-for-c-programmers.com
- Destroy All Values: Designing Deinitialization in Programming Languages - Faultloregankra.github.io
- Learn Rust the Dangerous Waycliffle.com
- Measure What You Optimizecliffle.com