use std::io;

fn main() {
    let mut input = String::new();

    io::stdin().read_line(&mut input)
        .expect("Failed to read line");

    println!("You entered: {}", input.trim());
}

这段 Rust 代码可以从终端读取用户输入,并将其打印出来。首先,我们创建一个可变字符串 input 来保存用户输入。然后,我们使用 stdin() 函数从标准输入读取数据,并将其存储在 input 变量中。最后,我们使用 println!() 宏将用户输入打印到控制台上。trim() 方法用于删除输入字符串中的空格和换行符。

给一个rust代码 获取终端输入的代码,不能瞎编

原文地址: https://www.cveoy.top/t/topic/fD2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录