gpt4 book ai didi

linux - 我可以以非规范的方式从 stdin 读取吗?

转载 作者:IT王子 更新时间:2023-10-29 00:40:33 24 4
gpt4 key购买 nike

有什么方法可以从 non-canonical mode 中的 stdin 中读取吗?在 Linux 下?非规范输入意味着在 stdin 上对 read() 的调用应在用户键入时立即返回,这不是默认行为,正如可以通过尝试看到的那样:

// Create a buffer
let mut buffer :[u8; 1] = [0];
// Loops over the input from stdin, one character a time
while io::stdin().read(&mut buffer).unwrap() > 0 {
println!("{:?}", buffer);
}

此代码等待用户按return 打印buffer 的内容。期望的行为是在用户键入时打印。在 Stdin 的文档中(由上面代码中的 stdin() 调用返回的结构),没有提及如何更改此默认行为。

最佳答案

不,不是没有外部包装箱或不安全的 FFI 代码。您可能想要使用 termios功能。具体参见 ICANONtcsetattr。 crate nix具有这些功能的绑定(bind)。参见 here有关如何在 Rust 中使用它们的示例。

关于linux - 我可以以非规范的方式从 stdin 读取吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33715597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com