#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를 적어 다시 spawn할 수 있게 하세요.
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에서 이 페이지 고치기. 마지막 수정