#E0133: 資源在移動或關閉後被使用
型別實作了 Drop 的值(File、socket、通道的 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 擁有它的檔案,接收它的指令會關閉這個檔案:把同一個 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 上編輯此頁 最後更新: