Updating a File In Rust

Hello Rustaceans,

Let assume I have something like this :

let X = 1;

while X!=10 {





In this kind of scenario, I notice X will be printed out 9 times on the terminal (loop effect) but it different case with file once the file pass through the first loop, it stop writing to it. So if the output on terminal is 1,2,3...9, the output inside the file is only 1 (I expected the nine values inside the file too but only see one).

I'm somehow confused, thinking it supposed to get updated.
Will be happy for more clarity.
Thanks a lot :pray:

No, you must be doing something wrong. Loops don't just stop out of the blue. Code is composable — a loop doesn't care what is in the loop body, the "loop with file operations in it" isn't special compared to "loop with printing to the terminal".

Please show us actual code – what you posted has syntax errors so it doesn't even compile.

1 Like
      let writer = std::fs::File::create(new.txt).unwrap();

let  mut  writer = BufWriter::new(writer);
let x = 1;
while x!= 7 {
println! ("{}", x);
writeln!(writer, "{:?}",x).unwrap();

This is the actual code. Trying to learn writing stuff to files. It run without error but not just getting the desire output. In the "new.txt" file I was expecting 1,2,3,4...6 but I only saw 6.

So sorry I didn't format the code, I typed with my phone


This might just be an issue of BufWriter not writing to the file until its internal buffer is full or until it is dropped. LineWriter might help solve this issue by writing each line to the file separately:

use std::{
    io::{prelude::*, LineWriter},

fn main() {
    let writer = File::create("new.txt").unwrap();
    let mut writer = LineWriter::new(writer);
    for x in 1..7 {
        println!("{}", x);
        writeln!(writer, "{:?}", x).unwrap();
1 Like

If you are only seeing 6 in your file then I would guess that the mistake you have made in your actual program that you ran is that you are opening the file repeatedly inside the loop. By default, that will overwrite the existing contents — if you want to append to a file you have to use OpenOptions::append (but it is usually not a good idea to open a single file repeatedly since that's slower than keeping it open).


After fixing a couple of compiler errors, this code works exactly as one can expect.


Thanks. Error fixed

1 Like

Thanks this approach works too

It worked too. Error fixed. I was doing something wrongly

@kpreid thanks once again. The solution works perfectly and I really appreciate your experience cos you knew exactly what the problem is even when I'm not that clear. Thanks a lot. And thanks to everyone too, I really learn a lot today. I hope to be better at asking clearly. :pray::pray::hugs::hugs:

1 Like

Here's a couple of tips:

  • When asking about a problem, if at all possible, include a complete, runnable program that when run, demonstrates the problem. It's very easy to, when trying to simplify a problem to explain it, remove the part that actually contains the bug.

    This isn't just helping the people trying to answer your question — if you make a simpler program that turns out not to have the problem, then you have learned more information about where the problem is; you know it must involve the parts you removed.

    This process often feels tedious, but it is worthwhile.

  • You don't need to reply to everyone individually. You can write one post and “@” anyone else you want to address. The thread will be more readable this way.


All noted. Thanks :hugs::hugs::hugs:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.