gpt4 book ai didi

input - 在使用 print! 的示例中可以简化!冲洗?

转载 作者:行者123 更新时间:2023-11-29 08:25:26 27 4
gpt4 key购买 nike

几天前,我通过阅读官方文档开始对 Rust 进行编程。现在,我正在尝试通过阅读 Brian P. Hogan(The Pragmatic Programmers)所著的“程序员练习”一书来挑战我对 Rust 的理解。

第一个练习是编写一个程序,要求用户输入姓名并使用该姓名打印问候语。输入、字符串连接和输出应分三个不同的步骤完成。

What is your name? Patrick
Hello, Patrick, nice to meet you.

名称将在与初始提示相同的行中输入。这是我的解决方案:

use std::io;
use std::io::Write;

fn main() {
print!("What is your name? ");
match io::stdout().flush() {
Ok(_) => print!(""),
Err(error) => println!("{}", error),
}
let mut name = String::new();
match io::stdin().read_line(&mut name) {
Ok(_) => {
name = name.trim().to_string();
if name.len() > 0 {
let greeting = "Hello, ".to_string() + &name + &", nice to meet you!".to_string();
println!("{}", greeting);
} else {
println!("No name entered, goodbye.");
}
}
Err(error) => println!("{}", error),
}
}

在我调用 flush 之前,print! 宏实际上并没有输出提示。 flush 需要错误处理,所以我需要同时处理 OkErr 情况。在 Ok 的情况下,没有什么有用的,所以我只是 print! 一个空字符串。

有没有更短的方法来处理这个问题?也许可以以某种方式跳过或简化错误处理,或者整个 print!/flush 方法都是错误的。 (一切正常,但毕竟我可以用 C 写得更短......)

最佳答案

正如其他人所说,请务必阅读 error handling chapter .

在大多数情况下,您不想使用println! 来报告错误。您应该从函数中返回错误并让调用者处理它,或者您应该使用 panic! 中止该线程并可能中止进程。

match io::stdout().flush() {
Ok(_) => print!(""),
Err(error) => println!("{}", error),
}

与其不打印任何内容(这是低效的),您可以...什么也不做:

match io::stdout().flush() {
Ok(_) => (),
Err(error) => println!("{}", error),
}

因为你不关心成功案例,你可以使用if let:

if let Err(error) = io::stdout().flush() {
println!("{}", error);
}

panic! 替换 println 会更好:

if let Err(error) = io::stdout().flush() {
panic!("{}", error);
}

这几乎就是Option::unwrap确实( source ),除了它在存在时也会返回成功的值:

pub fn unwrap(self) -> T {
match self {
Some(val) => val,
None => panic!("called `Option::unwrap()` on a `None` value"),
}
}

但是,使用 Option::expect更好这允许您指定额外的错误消息:

io::stdout().flush().expect("Unable to flush stdout");

应用两次:

use std::io::{self, Write};

fn main() {
print!("What is your name? ");

io::stdout().flush().expect("Unable to flush stdout");

let mut name = String::new();
io::stdin()
.read_line(&mut name)
.expect("Unable to read the line");

let name = name.trim();

if !name.is_empty() {
println!("Hello, {}, nice to meet you!", name);
} else {
println!("No name entered, goodbye.");
}
}

注意不需要重新分配一个String,你可以shadow name,也不需要使用format打印出东西。

从 Rust 1.26.0 开始,您还可以选择从 main 返回一个 Result:

use std::io::{self, Write};

fn main() -> Result<(), io::Error> {
print!("What is your name? ");
io::stdout().flush()?;

let mut name = String::new();
io::stdin().read_line(&mut name)?;

let name = name.trim();

if !name.is_empty() {
println!("Hello, {}, nice to meet you!", name);
} else {
println!("No name entered, goodbye.");
}

Ok(())
}

but I could write this shorter in C, after all...

我会鼓励/挑战你尝试这个。请注意,此程序中的每个内存分配都会被检查,处理标准输出的每个失败案例也是如此。许多人不知道 C 的 printf 返回错误代码,您应该检查一下。例如,尝试输出到已关闭的管道。

关于input - 在使用 print! 的示例中可以简化!冲洗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39174864/

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