小编nil*_*kar的帖子

使用 BufReader 读取 Rust 中的 JSON 文件时出错:特征绑定结果:std::io::Read 不满足

我正在尝试从文件中读取 JSON:

use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;

impl Params {
    pub fn new(raw_opt2: opt::Opt, path: String) -> Self {
        // Open the file in read-only mode with buffer.
        let file = File::open(path);
        let reader = BufReader::new(file);
        Self {
            opt_raw: raw_opt2,
            module_settings: serde_json::from_reader(reader).unwrap(),
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误:

error[E0277]: the trait bound `std::result::Result<std::fs::File, std::io::Error>: std::io::Read` is not satisfied
  --> src\params.rs:20:37
   |
20 |         let reader = BufReader::new(file);
   |                                     ^^^^ the trait `std::io::Read` is not implemented for `std::result::Result<std::fs::File, std::io::Error>`
   | …
Run Code Online (Sandbox Code Playgroud)

file-io json rust

2
推荐指数
1
解决办法
2109
查看次数

标签 统计

file-io ×1

json ×1

rust ×1