Hi all,
I am trying to print something like below,
lispy> hello
No you're a hello
lispy> my name is Dan
No you're a my name is Dan
lispy> Stop being so rude!
No you're a Stop being so rude!
lispy>
taken from here. First I will print lispy>
and wait for the user input, then after collecting it, I will print a new line No you are
userinput``.
fn main() {
loop {
print!("lispy>: ");
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(n) => {
println!("{} bytes read", n);
println!("{}", input);
}
Err(error) => println!("error: {}", error),
}
}
}
This code is not printing the lispy
and wainting for the user input. Rather it is printing the whole line including the user input with lispy
at once.
Any help.
leudz
2
I'd say you need to flush stdout.
3 Likes
Here is the updated code,
use std::io::stdout;
use std::io::{self, Write};
fn main() {
loop {
print!("lipsy>: ");
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(n) => {
println!("No you're {}", input);
}
Err(error) => println!("error: {}", error),
}
stdout().flush().unwrap();
}
}
I am getting,
Running `target/debug/lispwithrust`
good
lipsy>: No you're good
bad
lipsy>: No you're bad
nice
lipsy>: No you're nice
But I am expecting it,
lispy> hello
No you're a hello
lispy> my name is Dan
No you're a my name is Dan
lispy> Stop being so rude!
No you're a Stop being so rude!
leudz
4
No I meant flush right after print
.
2 Likes
Thanks by not writing the answer and just telling enough. This helped m learn a bit about flush.
4 Likes
system
Closed
6
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.