本页尚未翻译,以下为英文原文。
#E0139: stream view used past its stream
r.lines(), bytes, split, take, their async streams and incoming read through r and stay in the scope of r's owner: one is returned, stored past that scope, kept by a callee, or used after r was closed or moved. Use it inside the scope and collect what you need (try r.lines().collect()), or take the owning form (r.into_lines()), which owns r, closes it with itself and can be returned or moved into a task.
use std::fs::File;
use core::io::{BufReader, IoError, Lines};
fn lines_of(path: str) throws (IoError) -> Lines<BufReader<File>> {
using r = BufReader::new(try File::open(path)) {
return r.lines();
}
}
error[E0139]: this returns a view of `r` out of `r`'s scope --> main.nabi:6:9 | 6 | return r.lines(); | ^^^^^^ | --------- a view of `r` made here | = help: `r.lines()` reads through `r` and stays in its scope; use it there (`try r.lines().collect()`), or take the owning form `r.into_lines()`, which owns `r` and closes it with itself
Return the owning form. It is an io::Owned: it owns the reader, and closing it closes the reader:
use std::env;
use std::fs;
use std::fs::File;
use std::process;
use core::io::{BufReader, IoError, Lines, Owned};
fn lines_of(path: str) throws (IoError) -> Owned<Lines<BufReader<File>>, BufReader<File>> {
BufReader::new(try File::open(path)).into_lines()
}
fn main() throws (IoError) {
let p = env::temp_dir() / f"nabi_e0139_{process::id()}.txt";
try fs::write(p, "one\ntwo\n");
let ls = try lines_of(p.as_str());
try for l in ls {
println(l);
}
try ls.close();
try fs::remove_file(p);
}
one
two
Moving r ends its views too. After let kept = r;, passing r by value, capturing it with [move r] or storing it in a struct, a view made from r earlier can't be used:
use std::fs::File;
use core::io::{BufReader, IoError};
fn count(path: str) throws (IoError) -> usize {
let r = BufReader::new(try File::open(path));
let ls = r.lines();
let kept = r;
let n = 0usize;
try for l in ls { n += 1; }
try kept.close();
n
}
error[E0139]: `ls` is a view of `r` (made at line 6), used after `r` was moved at line 7 --> main.nabi:9:18 | 6 | let ls = r.lines(); | --------- a view of `r` made here 7 | let kept = r; | - `r` moved here ... 9 | try for l in ls { n += 1; } | ^^ | = help: `ls` reads through `r`, so it ends where `r` moves; finish with `ls` before moving `r`, or take the owning form `r.into_lines()` and move that instead
Finish with the view before r moves, or take the owning form and move that:
use std::env;
use std::fs;
use std::fs::File;
use std::process;
use core::io::{BufReader, IoError};
fn count(path: str) throws (IoError) -> usize {
let r = BufReader::new(try File::open(path));
let n = 0usize;
try for l in r.lines() { n += 1; }
let kept = r;
try kept.close();
n
}
fn main() throws (IoError) {
let p = env::temp_dir() / f"nabi_e0139m_{process::id()}.txt";
try fs::write(p, "one\ntwo\n");
println(f"{try count(p.as_str())}");
try fs::remove_file(p);
}
2
Related: E0134 resource not closed, E0133 resource used after it was moved or closed
Found a mistake? Edit this page on GitHub. Last updated