Moves, cloning, and copying - Rust for C-Programmers
How data is handled during assignment or function calls depends on its type. Rust distinguishes between moving, copying, and cloning. Types that manage resources on the heap, like String, Vec<T>, or Box<T>, use move semantics by default. When ownership is transferred (either through assignment to another variable or by passing the value to a function), the underlying resource is not duplicated; only the “control” (ownership) moves. The original variable binding becomes invalid. Move via Assignment: Move via Function Arguments: Passing a value to a function transfers ownership in the same way. Move via Function Return Values: Similarly, returning a value from a function moves ownership out of the function to the calling scope. What Actually Happens During a Move? When a value like String (or Vec<T>, Box<T>) is moved – either through assignment (let s2 = s1;) or by passing it by value to a function (takes_ownership(s1);) – the operation is very efficient at runtime. Remember that a Strin
Moves, cloning, and copying - Rust for C-Programmers 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 Rust for C-Programmers 6.2 Transferring Ownership: Move, Copy, and Clone How data is handled during assignment or function calls depends on its type. Rust distinguishes between moving , copying , and cloning . 6.2.1 Move Semantics Types that manage resources on the heap, like String , Vec<T> , or Box<T> , use move semantics by default. When ownership is transferred
Explore this link on the map →saved by
related reading
- What is Ownership? - The Rust Programming Languagedoc.rust-lang.org
- Moves - Rust: A Better C++ Than C++thecodedmessage.com
- What makes Rust special - Rust for C-Programmersrust-for-c-programmers.com
- Why Rust - Rust for C-Programmersrust-for-c-programmers.com
- A half-hour to learn Rustfasterthanli.me
- References and Borrowing - The Rust Programming Languagedoc.rust-lang.org
- Rust Language Cheat Sheetcheats.rs
- References and Borrowing - The Rust Programming Languagerust-book.cs.brown.edu
- What is Ownership? - The Rust Programming Languagerust-book.cs.brown.edu
- The Slice Type - The Rust Programming Languagedoc.rust-lang.org
- Destroy All Values: Designing Deinitialization in Programming Languages - Faultloregankra.github.io
- Variables and Mutability - The Rust Programming Languagedoc.rust-lang.org