flâneur — a map of the web's best reading

Moves, cloning, and copying - Rust for C-Programmers

rust-for-c-programmers.com · 1,351 words · saved by 1 readers

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