首页 > 解决方案 > Rust 命令行历史就像在终端中一样

问题描述

我有一些生锈的代码,可以循环获取当前输入

use std::io;

fn main() {
    let mut input = String::new();
    loop {
        io::stdin().read_line(&mut input).ok().expect("Couldn't read line");
        println!("hello {}", input)
    }
}

当在终端中按下向上箭头时,我如何让它将最近输入的输入放入输入中

终端示例:

>> echo "hello"
hello
>> echo "hello" [pressed up arrow]
hello

我想要它:

>> bucket
hello bucket
>> bucket [pressed up arrow]
hello bucket

标签: rust

解决方案


我认为您正在寻找的关键字可能是“readline”,它是用于在许多应用程序中实现此功能的 GNU 库。对于 Rust,例如rustyline等等。


推荐阅读