#E0133: 资源在移动或关闭之后又被使用
类型实现了 Drop 的值(File、套接字、通道的 Sender)只有一个所有者。它已经被移给另一个所有者或者已经关闭,却在这里再次被使用、移动或关闭。使用新的所有者,在关闭之前完成工作,或者在它移动之前用 .clone() 保留一份副本。拥有移入值的闭包([move x]),或者没有标为 Sync 的 fn(..) + Send 值,也是如此:把它传给 fn(..) + Send 参数(thread::spawn、task::spawn_blocking、par::join)就把它移交了一次。另外创建一个移入 .clone() 的闭包,或者在不移入任何值的 fn 值上写 + Send + Sync。每个字段都是自己的所有者,通过收窄和类型的绑定(if let x: Pair = p)也是如此:关闭 x.a 后,x.b 仍然是打开的。
use std::env;
use std::fs::File;
fn main() throws {
let f = try File::create(env::temp_dir() / "notes.txt");
let g = f;
try f.write_all("hi");
try g.close();
}
error[E0133]: `f` was moved at line 6, and is used here --> main.nabi:7:9 | 6 | let g = f; | - moved here 7 | try f.write_all("hi"); | ^ | = help: a `File` has one owner, and `f` gave it away; use the new owner, or keep a copy with `.try_clone()` before it moves
使用新的所有者:
use std::env;
use std::fs::File;
fn main() throws {
let f = try File::create(env::temp_dir() / "notes.txt");
let g = f;
try g.write_all("hi");
try g.close();
println("written");
}
written
拥有移入值的闭包([move x])也是如此:一旦把它交给 thread::spawn、task::spawn_blocking 或 par::join,再使用它就是这个错误,无论它经由什么途径传递(变量、结构体或元组的字段、列表、if 或 match 的分支、返回其实参的函数)。在一次调用中传入两次也算第二次移交:
let xs = [1, 2, 3];
let f = [move xs] || xs.len();
let (a, b) = par::join(f, f);
println(f"{a} {b}");
error[E0133]: `f` was given to a `par` job at line 3: it may own values moved into it (`[move ..]`), so it can be handed over only once --> main.nabi:3:27 | 3 | let (a, b) = par::join(f, f); | --------------- `f` given to the `par` job here | ^ | = help: the `par` job owns the closure and what it moved in; make another closure here (moving in a `.clone()` of what it needs), or, for a `fn` value that moves nothing in, write `+ Send + Sync` on its type
另外创建一个移入 .clone() 的闭包;对于不移入任何值的 fn 值,在它的类型上写 + Send + Sync,这样它就可以再次派生:
let xs = [1, 2, 3];
let ys = xs.clone();
let f = [move xs] || xs.len();
let g = [move ys] || ys.len();
let (a, b) = par::join(f, g);
println(f"{a} {b}");
3 3
async [move ..] 闭包和它的调用产生的 future 是同一个所有者。在这里依次 await 它们没有问题;把闭包或其中一个 future 交给 task::spawn 会把其他的一起带走,之后再使用它们就是这个错误。
Stdio::File 拥有它的文件,接收它的命令会关闭这个文件:把它交给两个命令,或者交给同一个命令的两个流(.stdout(s).stderr(s)),都是这个错误:
use std::fs::File;
use std::process::{Command, Stdio};
fn main() throws {
let s = Stdio::File(try File::create("out.txt"));
let a = try Command::new("echo").arg("a").stdout(s).status();
let b = try Command::new("echo").arg("b").stdout(s).status();
}
error[E0133]: `s` was moved at line 6, and is used here --> main.nabi:7:54 | 6 | let a = try Command::new("echo").arg("a").stdout(s).status(); | - moved here 7 | let b = try Command::new("echo").arg("b").stdout(s).status(); | ^ | = help: a `Stdio::File` belongs to the command it is given to, which closes it when it runs; give each command its own file (`Stdio::File(try f.try_clone())`)
给每个命令一个自己的文件 Stdio::File(try f.try_clone())。Stdio::Inherit、Null 和 Piped 不持有任何东西,可以交出任意多次。
结构体的每个字段都是自己的所有者,模式把和类型收窄后的成员(if let、match、let .. else)的每个字段也是如此:关闭 x.a 后,x.b 仍然是打开的,而再次关闭 x.a 就是这个错误:
use core::channel::{Sender, channel};
struct Pair { a: Sender<i64>; b: Sender<i64>; }
fn main() {
let (t1, _r1) = channel::<i64>();
let (t2, _r2) = channel::<i64>();
let p: Pair | none = Pair { a: t1, b: t2 };
if let x: Pair = p {
x.a.close();
x.a.close();
x.b.close();
}
}
error[E0133]: `x.a` was closed at line 10, and is used here --> main.nabi:11:9 | 10 | x.a.close(); | --- closed here 11 | x.a.close(); | ^^^ | = help: a `Sender<i64>` is released once; do the work before it is closed, or open a new one
每个字段只关闭一次:
use core::channel::{Sender, channel};
struct Pair { a: Sender<i64>; b: Sender<i64>; }
fn main() {
let (t1, r1) = channel::<i64>();
let (t2, r2) = channel::<i64>();
let p: Pair | none = Pair { a: t1, b: t2 };
if let x: Pair = p {
x.a.close();
x.b.close();
}
println(f"{r1.recv() == none} {r2.recv() == none}");
}
true true
相关错误码:E0134 资源没有关闭,E0105 值移交之后又被使用
发现错误? 在 GitHub 上编辑此页 最后更新: