gpt4 book ai didi

rust - 模式匹配 io::Result 时出错

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

这是我的代码:

fn main() {
use std::io::prelude::*;
use std::fs::File;

fn write_file() -> std::io::Result<()> {
let mut f = try!(File::create("foo.txt"));
try!(f.write_all(b"Hello, world!"));
}

match write_file() {
Ok => println!("File was written"),
Err(ex) => println!("{}", ex)
};
}

我有一个错误:

src/main.rs:11:9: 11:11 error: this pattern has 0 fields, but the corresponding variant has 1 field [E0023]
src/main.rs:11 Ok => println!("File was written"),
^~

为什么以及如何解决?

最佳答案

Ok => println!("File was written"),

应该是

Ok(()) => println!("File was written"),

因为 Ok 构造函数总是接受一个参数,即使该参数是 ()。如果您愿意,也可以使用 Ok(_)

关于rust - 模式匹配 io::Result 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33774985/

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