简体   繁体   English

与Rust借阅检查器战斗

[英]Fighting with the Rust borrow checker

This code looks like it would work fine to me, but the rust borrow checker doesn't like it: 这段代码对我来说似乎可以正常工作,但是rust借用检查器不喜欢它:

extern crate rustbox;

use std::error::Error;
use std::default::Default;

use rustbox::{Color, RustBox};
use rustbox::Key;

use std::fs::File;
use std::env;
use std::io::BufReader;
use std::io::BufRead;

fn display_screenful(rb: &RustBox, fr: BufReader<&'static File>, offset: usize) {
    for (rline, idx) in fr.lines().zip(0..).skip(offset).take(rb.height()) {
        match rline {
            Ok(line) => (*rb).print(0, idx, rustbox::RB_NORMAL, Color::White, Color::Black, &line),
            Err(_)   => (*rb).print(0, idx, rustbox::RB_NORMAL, Color::White, Color::Black, "")
        }
    }
}

fn main() {
    let rustbox = match RustBox::init(Default::default()) {
        Ok(v) => v,
        Err(e) => panic!(e),
    };

    let path = env::args().nth(1).unwrap();
    let file = match File::open(&path) {
        Ok(file) => file,
        Err(e)   => panic!(e)
    };
    let file_reader = BufReader::new(&file);

    display_screenful(&rustbox, file_reader, 0);
    rustbox.present();

    loop {
        match rustbox.poll_event(false) {
            Ok(rustbox::Event::KeyEvent(key)) => {
                match key {
                    Some(Key::Char('q')) => { break; }
                    Some(Key::Char(' ')) => {
                        display_screenful(&rustbox, file_reader, rustbox.height());
                        rustbox.present();
                    }
                    _ => { }
                }
            },
            Err(e) => panic!("{}", e.description()),
            _ => { }
        }
    }
}

I guess I could not use a separate function, and use two for loop parts, instead, but that isn't idiomatic Rust, nor is it good coding practice. 我想我不能使用一个单独的函数,而是使用两个for循环部分,但这不是惯用的Rust,也不是很好的编码习惯。 In fact, I've tried that, but it just tells me that I'm using a moved value. 实际上,我已经尝试过了,但这只是告诉我我正在使用移动值。 Here are some errors that I'm getting: 这是我遇到的一些错误:

   Compiling rusted v0.1.0 (file:///Users/christopherdumas/rusted)
src/main.rs:34:39: 34:43 error: `file` does not live long enough
src/main.rs:34     let file_reader = BufReader::new(&file);
                                                     ^~~~
note: reference must be valid for the static lifetime...
src/main.rs:33:7: 55:2 note: ...but borrowed value is only valid for the block suffix following statement 2 at 33:6
src/main.rs:33     };
src/main.rs:34     let file_reader = BufReader::new(&file);
src/main.rs:35
src/main.rs:36     display_screenful(&rustbox, file_reader, 0);
src/main.rs:37     rustbox.present();
src/main.rs:38
               ...
src/main.rs:45:53: 45:64 error: use of moved value: `file_reader` [E0382]
src/main.rs:45                         display_screenful(&rustbox, file_reader, rustbox.height());
                                                                   ^~~~~~~~~~~
src/main.rs:45:53: 45:64 help: run `rustc --explain E0382` to see a detailed explanation
src/main.rs:36:33: 36:44 note: `file_reader` moved here because it has type `std::io::buffered::BufReader<&'static std::fs::File>`, which is non-copyable
src/main.rs:36     display_screenful(&rustbox, file_reader, 0);
                                               ^~~~~~~~~~~
error: aborting due to 2 previous errors
Could not compile `rusted`.

To learn more, run the command again with --verbose.

Firstly, you shouldn't ask for a BufReader<&'static File> . 首先,您不应该要求提供BufReader<&'static File> You can't provide one. 您无法提供。 Ask instead for a BufReader<&'a File> for some lifetime 'a . 而是要求一个BufReader<&'a File>一生中的时间'a

fn display_screenful<'a>(rb: &RustBox, fr: BufReader<&'a File>, offset: usize)

or, for short, 或者,简而言之,

fn display_screenful(rb: &RustBox, fr: BufReader<&File>, offset: usize)

That's not enough either - you then end up moving file_reader into the function. 这也不足够-然后您最终将file_reader移入函数中。 You should borrow instead: 您应该改为借用:

fn display_screenful(rb: &RustBox, fr: &mut BufReader<&File>, offset: usize)

and then it compiles. 然后编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM