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);
}
相关推荐
m0_7482361118 分钟前
Calcite Web 项目常见问题解决方案
开发语言·前端·rust
SomeB1oody1 天前
【Rust自学】4.1. 所有权:栈内存 vs. 堆内存
开发语言·后端·rust
SomeB1oody2 天前
【Rust自学】4.2. 所有权规则、内存与分配
开发语言·后端·rust
SomeB1oody2 天前
【Rust自学】4.5. 切片(Slice)
开发语言·后端·rust
编码浪子2 天前
构建一个rust生产应用读书笔记6-拒绝无效订阅者02
开发语言·后端·rust
baiyu332 天前
1小时放弃Rust(1): Hello-World
rust
baiyu332 天前
1小时放弃Rust(2): 两数之和
rust
Source.Liu2 天前
数据特性库 前言
rust·cad·num-traits
编码浪子2 天前
构建一个rust生产应用读书笔记7-确认邮件1
数据库·rust·php
SomeB1oody2 天前
【Rust自学】3.6. 控制流:循环
开发语言·后端·rust