ownership
Rust Consume and return a transformed value
An API may accept ownership, modify the value and hand it back. This is useful when a transformation is designed to consume its input. For in-place updates without transfer, a mutable reference may be clearer. Expected output (not executed): A12-paid Why: The function takes ownership, changes the string and returns it to the caller. Common mistake: Assuming the original binding remains usable after a consuming call.
Formal Syntax & Structure
rust syntaxrust
fn update(mut value: String) -> String { ... }Working Example
Example Snippetrust
fn add_suffix(mut label: String) -> String { label.push_str("-paid"); label}fn main() { let order = String::from("A12"); let receipt = add_suffix(order); println!("{receipt}");}