如何在不显示用户输入的情况下输入密码?
fn main() {
println!("Type Your Password");
// I want to hide input here, and do not know how
let input = std::old_io::stdin().read_line().ok().expect("Failed to read line");
println!("{}", input);
}
答案 0 :(得分:9)
更新:您可以使用rpassword crate。引自自述文件:
将rpassword
条箱添加到Cargo.toml
:
[dependencies]
rpassword = "0.0.4"
然后使用read_password()
函数:
extern crate rpassword;
use rpassword::read_password;
fn main() {
print!("Type a password: ");
let password = read_password().unwrap();
println!("The password is: '{}'", password);
}
我怀疑你最好的选择是calling some C functions from rust:getpass (3)或其推荐的替代方案(参见 Getting a password in C without using getpass)。棘手的是,它当然不同于平台(如果你让它起作用,它就像箱子一样方便)。
根据您的要求,您还可以尝试使用ncurses-rs(crate“ncurses”);我没有对它进行测试,但看起来example 2可能会演示关闭回显输入。