新手 帮我看看我哪里写错了 不加换行打印不出 单行不加换行可以打印的
fn main() {
let total: i32 = 100000;
let barlen: i32 = 40;
println!("\n\n");
for i in 0..total + 1 {
// 计算百分比
let progress = i as f64 / total as f64 * 100 as f64;
// 完成进度
let complete = i as f64 / total as f64 * barlen as f64;
// 剩余进度
let incomplete = barlen - (complete as i32);
let mut s = String::new();
s.push_str("\x1b[42m");
s.push_str(&(" ".repeat(complete as usize)));
s.push_str("\x1b[49m");
s.push_str("\x1b[47m");
s.push_str(&(" ".repeat(incomplete as usize)));
s.push_str("\x1b[49m");
let progress = format!("{:.2} %", progress);
let down_space = " ".repeat(barlen as usize - progress.len());
let down = format!("{}{}", down_space, progress);
// 可以打印
let out = format!("\x1b[2A\r\x1b[0J{}\n{}\n", s, down);
// 打印不出来
// let out = format!("\x1b[1A\r\x1b[0J{}\n{}", s, down);
print!("{}", out);
}
}
1
共 3 条评论, 1 页
评论区
写评论谢谢 用stderr解决了。
--
👇
Aya0wind: stdout是有缓冲的,只有在换行,长度过长,或者你退出程序之后,才会往屏幕上输出,所以你不加换行就不行,所有语言都是这样。
你可以换成eprint!(),打印到stderr,这个是无缓冲的,就可以了。
stdout是有缓冲的,只有在换行,长度过长,或者你退出程序之后,才会往屏幕上输出,所以你不加换行就不行,所有语言都是这样。
你可以换成eprint!(),打印到stderr,这个是无缓冲的,就可以了。
ln具有flush io buffer的效果。