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 
4
  • 2
    Does this answer your question? How do I clear the current line of stdout? Commented Aug 6, 2021 at 21:28
  • 1
    Applicable: stackoverflow.com/q/2388090 (print \r to move the cursor to the beginning of the line) Commented Aug 6, 2021 at 21:32
  • @sk-pleasant-elias-holzmann , no, it doesn't fully cover my question Commented Aug 6, 2021 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. Commented Aug 6, 2021 at 21:48

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.