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
saved by
related reading
- Summary - Rust for C-Programmersrust-for-c-programmers.com
- Memory safety and ownership - Rust for C-Programmersrust-for-c-programmers.com
- Data types - Rust for C-Programmersrust-for-c-programmers.com
- What is Ownership? - The Rust Programming Languagedoc.rust-lang.org
- The program structure - Rust for C-Programmersrust-for-c-programmers.com
- Moves - Rust: A Better C++ Than C++thecodedmessage.com
- Summary - Rust for C-Programmersrust-for-c-programmers.com
- What makes Rust special - Rust for C-Programmersrust-for-c-programmers.com
- Summary - Rust for C-Programmersrust-for-c-programmers.com
- Summary - Rust for C-Programmersrust-for-c-programmers.com
- Rust for C programmers - Rust for C-Programmersrust-for-c-programmers.com
- Modules and crates - Rust for C-Programmersrust-for-c-programmers.com