error[E0382]: borrow of moved value: `u_own`
--> src/main.rs:4:20
|
2 | let u_own = String::new();
| ----- move occurs because `u_own` has type `String`, which does not implement the `Copy` trait
3 | let d_own = u_own;
| ----- value moved here
4 | println!("{}", u_own);
| ^^^^^ value borrowed here after move
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0382`.
1条答案
按热度按时间4ioopgfo1#
所有权永远不会移动。对于任何标记为
std::marker::Copy
的类型(我们说类型“是Copy”),赋值运算符不会移动所有权。它创建该值的副本。Rust中的基元类型默认为
Copy
,您可以在自己的任何类型上派生该标记,但应将其保留给小类型。简单枚举通常为Copy
。如果使用的类型不是
Copy
,则会出现预期的行为。例如,String
:playground