'How do I flush previous printed line? [duplicate]

I'm having a simple Rust program to emulate a timer:

use std::{io, thread, time};

fn main() {
    let start = time::Instant::now();
    let one_second = time::Duration::from_secs(1);
    let mut counter = 0;
    while counter <= 4 {
        thread::sleep(one_second);
        println!("ticking: {:.0}s", start.elapsed().as_secs_f32());
        counter += 1;
    }
}

It prints:

ticking: 1s
ticking: 2s
ticking: 3s
ticking: 4s
ticking: 5s

Now how do I change println! to erase the last printed line, so every time there is only one line showed in the terminal:

ticking: 1s at the 1st second

ticking: 2s at the 2nd second

...

then ticking: 5s only and no more other text in the end?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source