Home > Software design >  Rust all write to file methods give 'method not found in std::io::BufReader<std::fs::File>
Rust all write to file methods give 'method not found in std::io::BufReader<std::fs::File>

Time:06-07

I've been following tutorials, accepted answers, and the docs. They never worked for me since the beginning of learning, and now I'm stuck at it again. Imports:

  use std::io::prelude::*;
  use std::fs::{File, OpenOptions};
  use std::io::Read;
  use std::io::BufReader;
  use std::io::BufRead;
  use std::io::Write;

Code:

  let mut file_help = OpenOptions::new().append(true).create_new(true).open("n.txt").expect(".");
  let mut file_help = BufReader::new(file_help);

Loop for vec:

for i in d_call {file_help.write(format!("{}\n",i))};

In-loop variants that are giving out same errormethod not found in `std::io::BufReader<std::fs::File>:

file_help.write_all(format!("{}\n",i))

write!(file_help, "{}\n",i)

file_help.write(format!("{}\n",i.to_string()))

writeln!(file_help, "{}", i.to_string())
error[E0599]: no method named `write` found for struct `BufReader` in the current scope
  --> src/main.rs:21:19
   |
21 |         file_help.write(format!("{}\n", i.to_string()))
   |                   ^^^^^ method not found in `BufReader<File>`

Playground.

CodePudding user response:

The issue is actually quite simple. You are trying to write data to a BufReader. It can buffer file reads, but does not implement any write functionality. You likely want to use a std::io::BufWriter instead.

  • Related