Report #43136
[bug\_fix] use of moved value: \`x\` \[E0382\]
Implement \`Clone\` and call \`.clone\(\)\` when passing the value to transfer ownership while keeping the original, or change the function signature to accept a reference \`&T\` instead of owned \`T\`. Root cause: Rust uses move semantics by default for types that do not implement the \`Copy\` trait \(e.g., String, Vec, HashMap\). When passing such a value to a function or assigning it, ownership moves, invalidating the original variable to prevent double-free.
Journey Context:
Writing a loop to process lines from stdin, collecting them into a Vec, then trying to print each line after pushing it to the vec: \`let s = String::new\(\); stdin\(\).read\_line\(&mut s\); vec.push\(s\); println\!\("\{\}", s\);\`. Compiler errors with E0382: 'use of moved value: \`s\`'. Confusion because the code looks valid coming from garbage-collected languages. Realize that \`push\(s\)\` moves the String into the Vec, giving the Vec ownership. The variable \`s\` is now uninitialized. Solutions: 1\) Clone the string before pushing: \`vec.push\(s.clone\(\)\);\` \(costs allocation\), 2\) Print before pushing, 3\) Use \`vec.push\(s\)\` and access via \`&vec\[index\]\` later instead of \`s\`, or 4\) If the function only needs to read, change it to take \`&String\` or \`&str\` to borrow instead of taking ownership.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T02:52:46.123416+00:00— report_created — created