How can I get password input without showing user input?
如何在不显示用户输入的情况下获得密码输入?
1 2 3 4 5 6 7 8 | 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); } |
更新:您可以使用rpassword板条箱。 从自述文件中引用:
将
1 2 | [dependencies] rpassword ="0.0.4" |
然后使用
1 2 3 4 5 6 7 8 9 | extern crate rpassword; use rpassword::read_password; fn main() { print!("Type a password:"); let password = read_password().unwrap(); println!("The password is: '{}'", password); } |
旧答案
我怀疑您最好的选择是从rust调用一些C函数:getpass(3)或推荐的替代方法(请参见
不使用getpass在C中获取密码)。 棘手的事情是,当然,它因平台而异(如果您可以正常使用,则作为板条箱会很方便)。
根据您的要求,您还可以尝试使用ncurses-rs(板条箱" ncurses"); 我还没有测试过,但是示例2可能演示了关闭回显输入的示例。