我有这段代码,但它给了我一个我不明白的错误:
fn main() {
// --snip--
let mut unit = String::new();
std::io::stdin()
.read_line(&mut unit)
.expect("couldn't read line!");
let unit = unit.trim().to_lowercase();
dbg!(unit);
if unit == "f" {
println!("temperature in Celsius is : {}°c", f_to_c(temp));
} else if unit == "c" {
println!("temperature in Fahrenheit is : {}°f", c_to_f(temp));
} else {
println!("'{unit}' unit not supported!");
}
}
error[E0382]: borrow of moved value: `unit`
--> src/lib.rs:16:8
|
13 | let unit = unit.trim().to_lowercase();
| ---- move occurs because `unit` has type `String`, which does not implement the `Copy` trait
14 | dbg!(unit);
| ---------- value moved here
15 |
16 | if unit == "f" {
| ^^^^ value borrowed here after move
我让
unit
显式输入 String
但这不是原因。这个错误是怎么产生的?
dbg!
宏移动其参数。
您可以通过仅传递引用来使用它而不消耗参数,如下所示:
dbg!(&unit);
相反。