Rust- File

In Rust, file I/O is handled primarily through the std::fs and std::io modules. The std::fs module contains several functions for manipulating the filesystem, such as creating, removing, and reading files and directories. The std::io module contains traits, structs, and enums that can be used to handle input/output in a more abstract way, and is also used for error handling.

Here are a few examples of file operations in Rust:

1. Reading a File

rust 复制代码
use std::fs::File;
use std::io::Read;

fn main() -> std::io::Result<()> {
    let mut file = File::open("foo.txt")?;
    let mut contents = String::new();
    file.read_to_string(&mut contents)?;
    println!("{}", contents);
    Ok(())
}

This program opens the file foo.txt, reads its contents into a string, and then prints the string.

Note : The ? operator in Rust is used for error handling. It's a shorthand way to propagate errors up the call stack.

When you call a function that returns a Result type, it will return either an Ok(T) variant which contains the successful result, or an Err(E) variant which contains the error information.

If you use the ? operator on a Result value, it has the effect of "unwrapping" the Result if it's the Ok(T) variant and returning the contained value. However, if the Result is an Err(E) variant, the function will immediately return this Err from the current function.

In the line let mut file = File::open("foo.txt")?;, File::open("foo.txt") returns a Result<File>. If the file is opened successfully, ? unwraps the Result and file gets the File object. If there's an error (e.g., the file does not exist, or the program doesn't have permission to access it), the ? operator returns early from the function and gives the error.

The ? operator can only be used in functions that return a Result (or Option), because when an error occurs, ? returns it (it must return the same type as the function). So in the main function, you should specify that it returns a Result. If an error bubbles up to the main function, the error information will be printed to the standard error stream and the program will exit.

rust 复制代码
fn main() -> std::io::Result<()> {
    let mut file = File::open("foo.txt")?;
    // ...
    Ok(())
}

2. Writing to a File

rust 复制代码
use std::fs::File;
use std::io::Write;

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

This program creates a file named foo.txt, writes the byte string Hello, world! into the file, and then closes the file.

3. Working with Directories

rust 复制代码
use std::fs;

fn main() -> std::io::Result<()> {
    fs::create_dir("foo_dir")?; // create a directory
    fs::remove_dir("foo_dir")?; // remove the directory
    Ok(())
}

This program creates a directory named foo_dir, then removes it.

All of the functions used in these examples (File::open, File::create, fs::create_dir, etc.) can fail, for example, due to permissions, missing files, etc. They return a Result type, and by returning std::io::Result<()> from main(), these errors will automatically be handled by Rust: it will stop the program and print an error message.

Additionally, Rust has support for reading from and writing to files in a line-by-line or byte-by-byte manner, and includes many other features for advanced file I/O handling. These include file metadata, permissions, and more complex read/write operations.

A comprehensive case is as follows:

rust 复制代码
use std::fs::{self, OpenOptions};
use std::io::{Write, Read};

fn main() {
    let file = std::fs::File::open("data.txt");
    println!("文件打开\n{:?}", file);

    let file = std::fs::File::create("data2.txt").expect("创建失败");
    println!("文件创建成功{:?}", file);

    fs::remove_file("data.txt").expect("无法删除文件");
    println!("文件已删除");

    let mut file = OpenOptions::new().append(true).open("data2.txt").expect("失败");
    // file.write("\nRust Programming Language".as_bytes()).expect("写入失败");
    // println!("\n数据追加成功");

    file.write_all("Rust".as_bytes()).expect("失败");
    file.write_all("\nRust".as_bytes()).expect("失败");
    println!("\n数据写入成功");
    // // write_all并不会在写入后自动写入换行\n

    let mut file = std::fs::File::open("data2.txt").unwrap();
    let mut contents = String::new();
    file.read_to_string(&mut contents).unwrap();
    println!("{}", contents);
}
相关推荐
姜学迁2 小时前
Rust-枚举
开发语言·后端·rust
凌云行者2 小时前
rust的迭代器方法——collect
开发语言·rust
QMCY_jason9 小时前
Ubuntu 安装RUST
linux·ubuntu·rust
碳苯13 小时前
【rCore OS 开源操作系统】Rust 枚举与模式匹配
开发语言·人工智能·后端·rust·操作系统·os
zaim115 小时前
计算机的错误计算(一百一十四)
java·c++·python·rust·go·c·多项式
凌云行者1 天前
使用rust写一个Web服务器——单线程版本
服务器·前端·rust
cyz1410011 天前
vue3+vite@4+ts+elementplus创建项目详解
开发语言·后端·rust
超人不怕冷1 天前
[rust]多线程通信之通道
rust
逢生博客1 天前
Rust 语言开发 ESP32C3 并在 Wokwi 电子模拟器上运行(esp-hal 非标准库、LCD1602、I2C)
开发语言·后端·嵌入式硬件·rust
Maer091 天前
WSL (Linux)配置 Rust 开发调试环境
linux·运维·rust