我希望编写一个提示函数,它将传入的字符串发送到标准输出(stdout),然后返回从标准输入(stdin)读取的字符串。我该如何测试它?
以下是这个函数的示例:
以下是这个函数的示例:
fn prompt(question: String) -> String {
let mut stdin = BufferedReader::new(stdin());
print!("{}", question);
match stdin.read_line() {
Ok(line) => line,
Err(e) => panic!(e),
}
}
以下是我的测试尝试
#[test]
fn try_to_test_stdout() {
let writer: Vec<u8> = vec![];
set_stdout(Box::new(writer));
print!("testing");
// `writer` is now gone, can't check to see if "testing" was sent
}
println!
只是调用了writeln!(io::stdout(),...
吗? - Frank Meulenaarprintln!
最终使用 stdout,但不完全是那种方式。反过来问——为什么它会失去性能呢? - Shepmaster