关于rust的fs标准库导入和引用

发布于 2022-09-01 22:28:06 字数 738 浏览 26 评论 0

各位学习rust的朋友,我在按照官方的文档试rust的标准fs库时遇到下面的问题:

rust 1.4.0 stable win7 64位

尝试fs库的时候,如下代码编译报错

   use std::io::prelude::*;
    // use std::io::write_all;
    use std::fs::File;
    
    fn main() {
        let mut f File::create("test.txt");
        f.wirte_all(b"hello");
    }

报错信息:
main.rs:7:4: 7:23 error: no method named wirte_all found for type core::result::Result<std::fs::File, std::io::error::Error> in the current scope
main.rs:7 f.wirte_all(b"hello");

            ^~~~~~~~~~~~~~~~~~~

error: aborting due to previous error
Could not compile test.



各位知道是什么原因么?



如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半仙 2022-09-08 22:28:07

大哥,你打字打错了, 不是wirte_all, 是write_all

神爱温柔 2022-09-08 22:28:06

报错信息写得很清楚了,类型不匹配!

File::create("test.txt")返回值的类型是core::result::Result<std::fs::File, std::io::error::Error>

所以你需要做一个match


fn main() {
    let mut file = File::create('foo.txt');
    
    match file {
        Ok(mut stream) => {
            stream.write_all(b"bar");
        }
        Err(err) => {
            panic!(err);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文