Rust 编译器,不仅能够通过类型安全在编译期检测出这一错误,告诉你这个错误产生的原因:"may outlive borrowed value"(我们暂且不管它是什么意思),并且,它还进一步推荐你加入 "move" 解决这个错误。为了方便你进一步了解错误详情,它还贴心地给出一个命令行 "rustc --explain E0373",让你可以从知识库中获取更多的信...
borrows 'data', which is owned by the current function --> src/main.rs:6:19 | 6 | thread::spawn(|| { data = 500; }); | ^^ --- 'data' is borrowed here | | | may outlive borrowed value 'data' | note: function requires argument type to outlive ''static' --> src/main.rs...
error[E0373]: closure may outlive the current function, but it borrows `factor`, which is owned by the current function --> src/main.rs:2:5 | 2 | |input: i32| { | ^^^ may outlive borrowed value `factor` 3 | input * factor | --- `factor...
may outlive borrowed value ,由此可知闭包默认使用的是borrow ,而不是move,对应的Trait是 Fn,如果是使用move关键字,对应的Trait就会是FnOnce 继续看这句报错,回过头看代码,可知的是,在线程中的作用域使用num_f这个变量时,由于num_f也在外面的作用域,Rust编译器不能确定在运行时外面是否会修改这个变量,对于此种场...
error[E0373]: closure may outlive the current function, but it borrows `v`, which is owned by the current function--> src/main.rs:54:32 | 54 | let handle = thread::spawn(||{| ^^may outlive borrowed value `v`55 | println!("here is a vector: {:?}",v);| -`v` is borrowed...
(|| { | ^^ may outlive borrowed value `name` 7 | println!("hello {}", name); | --- `name` is borrowed here | help: to force the closure to take ownership of `name` (and any other referenced variables), use the `move` keyword, as shown: | let t = thread::spawn(move |...
("Hello: {}", name); | ^^ --- `name` is borrowed here | | | may outlive borrowed value `name` | note: function requires argument type to outlive `'static` --> src\main.rs:4:19 | 4 | let handler = std::thread::spawn(hello_from_thread); | ^^^ help: to force the closu...
};println!("Value: {}", immut_val);//okfn_closure();//ok// cannot borrow mutably because it is already borrowed immutably// immut_val.push_str("-push");// fn_closure();} 因此,由于值没有被修改,闭包通过不可变的借用来捕获变量。这意味着我们可以对变量进行其他不可变引用. ...
may outlive borrowed value ,由此可知闭包默认使用的是borrow ,而不是move,对应的Trait是 Fn,如果是使用move关键字,对应的Trait就会是FnOnce 继续看这句报错,回过头看代码,可知的是,在线程中的作用域使用num_f这个变量时,由于num_f也在外面的作用域,Rust编译器不能确定在运行时外面是否会修改这个变量,对于此种场...
error[E0373]: closure may outlive the current function, but it borrows `v`, which is owned by the current function --> src/main.rs:6:32 6 | let handle = thread::spawn(|| { | ^^ may outlive borrowed value `v` 7 | println!("Here's a vector: {:?}", v); ...