docs

#E0133: resource used after it was moved or closed

A value whose type implements Drop (a File, a socket, a channel Sender) has one owner. It was moved to another owner or closed, and is used, moved or closed again here. Use the new owner, do the work before it is closed, or keep a copy with .clone() before it moves. A closure that owns values moved into it ([move x]), or a fn(..) + Send value not marked Sync, is the same: passing it to a fn(..) + Send parameter (thread::spawn, task::spawn_blocking, par::join) hands it over once. Make another closure that moves in a .clone(), or write + Send + Sync on a fn value that moves nothing in. Each field is its own owner, also through a binding that narrows a sum (if let x: Pair = p): closing x.a leaves x.b open.

nabi
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 the new owner:

nabi
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

A closure that owns values moved into it ([move x]) is the same: once it is handed to thread::spawn, task::spawn_blocking or par::join, using it again is this error, however it travels (a variable, a struct or tuple field, a list, an if or match arm, a function that returns its argument). Passing it twice to one call is a second hand-over too:

nabi
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

Make a second closure that moves in a .clone(), or, for a fn value that moves nothing in, write + Send + Sync on its type so it can be spawned again:

nabi
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

An async [move ..] closure and the futures its calls make are one owner. Awaiting them here, one after another, is fine; handing the closure or one of its futures to task::spawn takes the others along, and using them afterwards is this error.

A Stdio::File owns its file, and the command it is given to closes it: giving one to two commands, or to two streams of one command (.stdout(s).stderr(s)), is this error:

nabi
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())`)

Give each its own file, Stdio::File(try f.try_clone()). Stdio::Inherit, Null and Piped hold nothing and can be given any number of times.

Each field of a struct is its own owner, and so is each field of the member a pattern narrows a sum to (if let, match, let .. else): closing x.a leaves x.b open, and closing x.a again is this error:

nabi
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

Close each field once:

nabi
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

Related: E0134 resource not closed, E0105 value used after it was handed over

Found a mistake? Edit this page on GitHub. Last updated