Rust- 错误处理

Rust approaches error handling with the aim of being explicit about possible error conditions. It separates the concerns of "normal" return values and "error" return values by using a specific set of types for each concern. Rust's primary tools for expressing these concepts are the Result and Option types, along with the unwrap method.

  1. The Result type : This is an enum that represents either successful (Ok) or unsuccessful (Err) computation. Functions that can fail generally return a Result variant.

    Here's a simple example:

    rust 复制代码
    fn divide(numerator: f64, denominator: f64) -> Result<f64, &str> {
        if denominator == 0.0 {
            Err("Cannot divide by zero")
        } else {
            Ok(numerator / denominator)
        }
    }

    In this case, if you try to divide by zero, the function will return an error wrapped in the Err variant. Otherwise, it will return the division result wrapped in the Ok variant.

  2. The Option type : This is another enum that represents a value that could be something (Some) or nothing (None). It's often used when you have a situation where a value could be absent or present.

    For example:

    rust 复制代码
    fn find_word_starting_with<'a, 'b>(sentence: &'a str, initial: &'b str) -> Option<&'a str> {
        for word in sentence.split_whitespace() {
            if word.starts_with(initial) {
                return Some(word);
            }
        }
        None
    }

    This function tries to find a word starting with a specific string in a sentence. If it finds such a word, it returns Some(word), otherwise, it returns None.

  3. The unwrap method : Both Option and Result types have an unwrap method, which is used to get the value out. If the instance of Option is a Some variant, unwrap will return the value inside the Some. If the instance is a None, unwrap will panic.

    Similarly, for Result, if the instance is an Ok, unwrap will return the value inside the Ok. If the instance is an Err, unwrap will panic.

    rust 复制代码
    let maybe_number: Option<i32> = Some(42);
    println!("{}", maybe_number.unwrap()); // Prints 42
    
    let definitely_not_a_number: Option<i32> = None;
    println!("{}", definitely_not_a_number.unwrap()); // This will panic

    It's generally advised to avoid using unwrap in most situations, as it can lead to your program crashing. Instead, you should handle errors appropriately using pattern matching or methods like unwrap_or, unwrap_or_else, expect, etc., which allow you to provide alternative values or actions in case of an error.

  4. The ? operator : This is a convenient way to propagate errors upwards in the call stack. When you use ? on a Result value, it does pretty much the same as a match expression would do: if the value is Ok, it unwraps it; if it's Err, it returns it from the current function. This allows for more concise error handling.

rust 复制代码
fn main() {
    match foo() {
        Ok(_) => println!("Success"),
        Err(e) => println!("Error: {}", e),
    }
}

fn foo() -> Result<(), &'static str> {
    bar()?;
    Ok(())
}

fn bar() -> Result<(), &'static str> {
    Err("Some error")
}

In the foo function, bar()? will return the error from bar, thus ending the foo function early.

These are the basic building blocks of error handling in Rust. Rust also provides advanced features like creating your own error types, using the From trait for flexible error conversions, etc., but those are more advanced topics.

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

fn main() {
    panic!("出错了");
    println!("Hello Rust");

    // panic 程序立即退出,退出的时候调用者抛出退出原因。

    // 数组越界
    let v = vec!["Rust", "Programming", "Language"];
    println!("{}", v[5]); // thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 5'

    // 打开文件出错
    let f = File::open("abc.txt");
    println!("{:?}", f); // Err(Os { code: 2, kind: NotFound, message: "系统找不到指定的文件。" })

    // unwrap expect
    let result = is_even(6).unwrap();
    println!("结果{}", result);

    let result = is_even(11).unwrap();
    println!("结果{}", result);

    // unwrap()是Result<T, E>的方法,实例上调用此方法时,如果是Ok,就返回Ok中的对象
    // 如果是Err枚举,在运行时会panic

    // expect()接收msg::&str作为参数,可以自定义报错信息。
}

fn is_even(no: i32) -> Result<bool, String> {
    return if no % 2 == 0 {
        Ok(true)
    } else {
        Err("输入的值不是偶数".to_string())
    };
}
相关推荐
萧曵 丶7 小时前
Rust 仿射类型(Affine Types)
rust·仿射类型
寻月隐君10 小时前
Rust核心利器:枚举(Enum)与模式匹配(Match),告别空指针,写出优雅健壮的代码
后端·rust·github
泊浮目1 天前
生产级Rust代码品鉴(一)RisingWave一条SQL到运行的流程
大数据·后端·rust
得物技术1 天前
从Rust模块化探索到DLB 2.0实践|得物技术
rust
寻月隐君1 天前
不止于后端:Rust 在 Web 开发中的崛起之路 (2024数据解读)
后端·rust·github
萧曵 丶3 天前
Rust 所有权系统:深入浅出指南
开发语言·后端·rust
GetcharZp3 天前
彻底告别数据焦虑!这款开源神器 RustDesk,让你自建一个比向日葵、ToDesk 更安全的远程桌面
后端·rust
solohoho3 天前
Rust:结构体、方法生命周期标注核心要义
rust
Humbunklung3 天前
Rust枚举:让数据类型告别单调乏味
开发语言·后端·rust
柑木3 天前
Rust-开发应用-如何实现单例
后端·rust