偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

理解 Rust 中的不可恢復錯誤和可恢復錯誤

開發(fā) 前端
在 Rust 中,不可恢復錯誤指的是程序無法安全繼續(xù)執(zhí)行的情況。這些錯誤通過 panic!? 宏來處理。當 panic! 被觸發(fā)時,應(yīng)用程序?qū)⑼V箞?zhí)行,并展開棧,這通常會導致進程終止。

在 Rust 中,錯誤分為兩類:可恢復錯誤和不可恢復錯誤。對于可恢復錯誤,我們使用 Result 枚舉,而對于不可恢復錯誤,我們使用 panic! 宏。

Rust 中的不可恢復錯誤:Panic 宏

在 Rust 中,不可恢復錯誤指的是程序無法安全繼續(xù)執(zhí)行的情況。這些錯誤通過 panic! 宏來處理。當 panic! 被觸發(fā)時,應(yīng)用程序?qū)⑼V箞?zhí)行,并展開棧,這通常會導致進程終止。

示例:索引越界

例如,如果你嘗試訪問超出向量范圍的索引,將會引發(fā) panic:

let names = vec!["Sunny", "Hugo", "Charlie"];
names[10]; // 這將導致 panic

// thread 'main' panicked at src/main.rs:5:10:
// index out of bounds: the len is 3 but the index is 10

在數(shù)據(jù)無效或無法從錯誤中恢復的情況下,你可以手動調(diào)用 panic! 宏:

panic!("The app cannot continue, please fix data");

// thread 'main' panicked at src/main.rs:2:5:
// The app cannot continue, please fix data

不可恢復錯誤總結(jié)

當程序因關(guān)鍵問題(如數(shù)據(jù)無效或違反程序假設(shè)的情況)而無法繼續(xù)時,就會發(fā)生不可恢復錯誤。panic! 宏用于在這些錯誤發(fā)生時停止程序執(zhí)行。

Rust 中的可恢復錯誤:Result 枚舉

與此相對,可恢復錯誤是指程序可以處理并繼續(xù)運行的錯誤。這類錯誤通過 Result 枚舉來處理。

Result 枚舉有兩個變體:

  • Ok(T): 表示成功,包含結(jié)果值
  • Err(E): 表示失敗,包含錯誤值
enum Result<T, E> {
  Ok(T),
  Err(E)
}

示例:文件未找到

如果你嘗試打開一個不存在的文件,可以在不崩潰程序的情況下處理錯誤。以下是使用 Result 枚舉和 match 語句的示例:

use std::fs::File;

fn main() {
    let file = File::open("bad_file.png");
    match file {
        Ok(f) => {
            println!("File found {:?}", f);
        },
        Err(e) => {
            println!("File not found {:?}", e);
        }
    }

    println!("App is still running...");
}

// File not found Os { code: 2, kind: NotFound, message: "The system cannot find the file specified." }
// App is still running...

如你所見,程序沒有 panic,而是通過打印錯誤信息來處理錯誤,應(yīng)用程序繼續(xù)運行。

錯誤處理的輔助方法

你可能見過 Rust 中的 unwrap() 方法,并想知道它的作用。unwrap() 是處理 Result 類型的快捷方式,但存在風險。如果結(jié)果是 Ok,unwrap() 返回值。然而,如果結(jié)果是 Err,unwrap() 將導致 panic。

你可以用 unwrap() 替換 match 語句:

let file = File::open("bad_file.png").unwrap();

// thread 'main' panicked at src/main.rs:4:43:
// called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "The system cannot find the file specified." }

如你所見,如果文件未找到,程序?qū)?panic 并停止運行。

第二個選項是 expect()。

expect() 方法類似于 unwrap(),但允許你提供自定義錯誤信息。如果你希望提供更多關(guān)于錯誤的上下文,而不僅僅依賴于默認的 panic 信息,這將非常有用。

let file = File::open("bad_file.png").expect("Ooops something went wrong");

// thread 'main' panicked at src/main.rs:4:43:
// Ooops something went wrong: Os { code: 2, kind: NotFound, message: "The system cannot find the file specified." }

結(jié)論

  • 不可恢復錯誤:在程序無法繼續(xù)的情況下使用 panic!。這些錯誤會停止程序的執(zhí)行。
  • 可恢復錯誤:使用 Result 枚舉來處理程序可以繼續(xù)的錯誤。你可以使用 match、unwrap() 或 expect() 來處理這些錯誤。
責任編輯:武曉燕 來源: Rust開發(fā)筆記
相關(guān)推薦

2017-04-10 14:56:22

windows重啟

2023-10-27 14:54:04

CipherWindows

2021-04-12 11:25:31

手機數(shù)據(jù)設(shè)備

2015-09-22 09:26:21

DBA失誤備份數(shù)據(jù)丟失

2022-03-25 08:00:00

Kubernetes備份集群

2023-01-07 14:48:09

3D信息

2009-11-18 09:39:06

Oracle介質(zhì)恢復

2017-03-29 19:00:20

災(zāi)難恢復IT停機虛擬化

2018-06-15 09:26:13

RTORPO差異

2018-08-21 12:14:07

華為云

2013-08-08 10:10:06

備份策略全備份增量備份

2023-09-07 07:53:21

JavaScriptGoRust

2009-04-13 09:26:00

IP地址網(wǎng)絡(luò)管理故障

2023-04-17 07:41:02

Rust網(wǎng)絡(luò)數(shù)據(jù)

2023-08-07 06:39:03

網(wǎng)絡(luò)傳輸

2023-10-28 16:30:19

Golang開發(fā)

2010-05-31 10:35:58

機柜數(shù)據(jù)中心

2011-03-24 17:49:47

數(shù)據(jù)庫恢復

2011-07-12 17:55:28

尾日志備份

2017-06-07 19:18:56

Oracle實例恢復前滾和回滾
點贊
收藏

51CTO技術(shù)棧公眾號