【问题标题】:no method named flush found for type std::io::Stdout in the current scope在当前范围内找不到类型 std::io::Stdout 的名为 flush 的方法
【发布时间】:2017-03-16 12:43:48
【问题描述】:

我发现的所有有关刷新的文档都表明刷新标准输出的正确方法如下:

std::io::stdout().flush().expect("some error message");

这导致

no method named flush found for type std::io::Stdout in the current scope

我做错了什么?

【问题讨论】:

    标签: rust


    【解决方案1】:

    您需要为Stdout 导入实现flush 方法的特征。

    根据文档:

    因此:

    use std::io::Write; // <--- bring the trait into scope
    
    fn main() {
        std::io::stdout().flush().expect("some error message");
    }
    

    Playground example

    【讨论】:

    • 为什么有必要这样做?
    【解决方案2】:

    谁能告诉我我做错了什么?

    是的; 编译器已经这样做了

    fn main() {
        std::io::stdout().flush().expect("some error message");
    }
    
    error[E0599]: no method named `flush` found for type `std::io::Stdout` in the current scope
     --> src/main.rs:3:23
      |
    3 |     std::io::stdout().flush().expect("some error message");
      |                       ^^^^^
      |
      = help: items from traits can only be used if the trait is in scope
      = note: the following trait is implemented but not in scope, perhaps add a `use` for it:
              candidate #1: `use std::io::Write;`
    

    强调helpnote 行 - use std::io::Write

    大家一起:

    use std::io::Write;
    
    fn main() {
        std::io::stdout().flush().expect("some error message");
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-22
      • 2023-01-15
      • 2018-07-08
      • 2019-11-16
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 2021-09-21
      相关资源
      最近更新 更多