1

I want to update otput. For example, i have a timer and every second program prints new second. But i need it dosn't create new line it should replace last line.

Code above:

let mut n = 0;
loop {
    println!("Value: {}", n);
    n += 1;
}

prints value of n in new line:

Value: 0
Value: 1
Value: 2
Value: 3
Value: 4
Value: 5...

I need to update value in one line:

Value: 0
Value: 1
Value: 2

I have example:

use std::thread::sleep;
use std::time::Duration;

fn main() {
    for i in 0..5 {
        print!("{}", i);
        sleep(Duration::from_millis(100000));
    }
}

i need to print value of variable in one line. after run:

$ cargo run

It prints form 0 to 5 in one line

First iteration

$ cargo run
0

Second iteration

$ cargo run
1

Third iteration

$ cargo run
2
joxig38172
  • 11
  • 3
  • 2
    Does this answer your question? [How do I clear the current line of stdout?](https://stackoverflow.com/questions/28823788/how-do-i-clear-the-current-line-of-stdout) – Elias Holzmann Aug 06 '21 at 21:28
  • 1
    Applicable: https://stackoverflow.com/q/2388090 (print `\r` to move the cursor to the beginning of the line) – E_net4 Aug 06 '21 at 21:32
  • @sk-pleasant-elias-holzmann , no, it doesn't fully cover my question – joxig38172 Aug 06 '21 at 21:32
  • 1
    What you're wanting to do is overwrite the same line every time you print. The way that you do that is by moving the cursor back to the initial position before the next output happens. The linked duplicates explain various options for doing that. – Herohtar Aug 06 '21 at 21:48

0 Answers0