docs

#E0134: 닫지 않은 자원

이 값을 닫는 일은 실패할 수 있으므로(타입이 io::Close를 구현합니다) 저절로 닫히지 않습니다. 어떤 경로에서 아직 열린 채로 스코프를 떠나거나, 바뀌거나, try?가 버리는 에러와 함께 버려집니다. 떠나기 전에 try x.close();를 넣거나, using x = .. { .. }로 묶거나, 에러를 잡아 그것이 담은 것을 닫으세요.

nabi
use std::env;
use std::fs::File;

fn main() throws {
    let f = try File::create(env::temp_dir() / "notes.txt");
    try f.write_all("hi");
}
error[E0134]: `f` holds a `File` that is never closed
  --> main.nabi:5:17
   |
 5 |     let f = try File::create(env::temp_dir() / "notes.txt");
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 6 |     try f.write_all("hi");
   |         ----------------- it is still open when it leaves here
   |
   = help: closing a `File` can fail, so it is not closed for you: add `try f.close();` before it leaves its scope, or bind it with `using f = .. { .. }`

스코프를 떠나기 전에 닫으세요.

nabi
use std::env;
use std::fs::File;

fn main() throws {
    let f = try File::create(env::temp_dir() / "notes.txt");
    try f.write_all("hi");
    try f.close();
    println("written");
}
written

호출이 빌리기만 하는 임시 값도 같습니다. (try File::open(p)).read_to_str()나 BufReader::new(try File::open(p)).lines()에서는 호출이 끝난 뒤 파일을 가진 쪽이 없으므로 아무도 닫지 않습니다.

nabi
use std::fs::File;
use core::io::IoError;

fn whole(path: str) throws (IoError) -> str {
    try (try File::open(path)).read_to_str()
}
error[E0134]: the `File` made here is never closed: this call only borrows it
  --> main.nabi:5:14
   |
 5 |     try (try File::open(path)).read_to_str()
   |              ^^^^^^^^^^^^^^^^
   |
   = help: closing a `File` can fail, so it is not closed for you: bind it with `using` (`using x = .. { .. }`), or bind it and close it (`let x = ..; .. try x.close();`)

using으로 묶으세요. 블록이 끝날 때 닫힙니다.

nabi
use std::env;
use std::fs;
use std::fs::File;
use std::process;
use core::io::IoError;

fn whole(path: str) throws (IoError) -> str {
    using f = try File::open(path) { try f.read_to_str() }
}

fn main() throws (IoError) {
    let p = env::temp_dir() / f"nabi_e0134_{process::id()}.txt";
    try fs::write(p, "hi");
    println(try whole(p.as_str()));
    try fs::remove_file(p);
}
hi

관련 코드: E0133 옮기거나 닫은 뒤에 쓴 자원, E0066 Drop 없는 값의 using

잘못된 곳이 있나요? GitHub에서 이 페이지 고치기. 마지막 수정