欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Rust中non_exhaustive的enum使用確保程序健壯性

 更新時(shí)間:2023年11月03日 10:29:17   作者:da_miao_zi  
這篇文章主要為大家介紹了Rust中non_exhaustive的enum使用確保程序健壯性示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

Rust中non_exhaustive的enum

所謂non_exhaustive的enum就是定義中帶有#[non_exhaustive]enum,如

#[non_exhaustive]
pub enum Error {
    Message(String),
    Other,
}

在定義了這個(gè)enum的crate中,non_exhaustive沒有任何效果。

let error = Error::Other;
// Non-exhaustive enums can be matched on exhaustively within the defining crate.
match error {
    Error::Message(ref s) => { },
    Error::Other => { },
}

但若引用的crate中存在non_exhaustiveenum,會(huì)發(fā)生什么呢?

以libpnet這個(gè)crate為例

https://docs.rs/pnet/latest/pnet/#這個(gè)頁面給出的示例代碼中,可以看到這么一段

// Create a new channel, dealing with layer 2 packets
let (mut tx, mut rx) = match datalink::channel(&interface, Default::default()) {
    Ok(Ethernet(tx, rx)) => (tx, rx),
    Ok(_) => panic!("Unhandled channel type"),
    Err(e) => panic!("An error occurred when creating the datalink channel: {}", e)
};

datalink::channel()的返回值類型為Result<Channel, Error>,而Channel的類型是一個(gè)enum,

pub enum Channel {
    Ethernet(Box<dyn DataLinkSender, Global>, Box<dyn DataLinkReceiver, Global>),
}

atalink::channel()返回結(jié)果分析

如果datalink::channel()返回的是成功的結(jié)果(類型為Channel),將與第一個(gè)Ok(Ethernet(tx, rx))模式的前半部分匹配(因?yàn)?code>Ok()就對(duì)應(yīng)成功的結(jié)果)。

而根據(jù)Channel的定義,Channel::Ethernet(tx, rx)是該enum唯一的成員(variant),所以只要是成功的結(jié)果,就應(yīng)該匹配第1個(gè)模式,否則就匹配最后的Err(e)這個(gè)模式。

這看起來滿足Rust中的匹配必須是窮舉式的(exhaustive)這一要求。因此似乎第2個(gè)模式Ok(_) => ,就顯得多余了。但若去掉這行,編譯時(shí)就會(huì)報(bào)錯(cuò)Ok(_) not covered

error[E0004]: non-exhaustive patterns: `Ok(_)` not covered
  --> src/main.rs:33:31
   |
33 |     let (_tx, mut rx) = match datalink::channel(&interface, Default::default()) {
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pattern `Ok(_)` not covered
   |
note: `Result<Channel, std::io::Error>` defined here
...
   |
   = note: not covered
   = note: the matched value is of type `Result<Channel, std::io::Error>`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
   |
38 ~         },
39 +         Ok(_) => todo!()
   |

For more information about this error, try `rustc --explain E0004`.

這背后的原因就是因?yàn)?code>enum Channel是non_exhaustive的,其完整定義是

// https://docs.rs/pnet_datalink/0.34.0/src/pnet_datalink/lib.rs.html#99
/// A channel for sending and receiving at the data link layer.
#[non_exhaustive]
pub enum Channel {
    /// A datalink channel which sends and receives Ethernet packets.
    Ethernet(Box<dyn DataLinkSender>, Box<dyn DataLinkReceiver>),
}

而我們又在外部引用了包含這個(gè)enum的crate。解決的辦法就是加入Ok(_) => {...}。

感覺這應(yīng)該算Rust確保程序健壯性(魯棒性)的一方面,在編譯階段就報(bào)出潛在的錯(cuò)誤——咱也不知道引用的crate什么時(shí)候會(huì)在enum中增加新的成員。

參考

https://doc.rust-lang.org/beta/reference/attributes/type_syst... Type system attributes

https://docs.rs/pnet/latest/pnet/#examples This (fairly useless) code implements an Ethernet echo server.

以上就是Rust中non_exhaustive的enum使用確保程序健壯性的詳細(xì)內(nèi)容,更多關(guān)于Rust 的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Rust錯(cuò)誤處理之`foo(...)?`的用法與錯(cuò)誤類型轉(zhuǎn)換小結(jié)

    Rust錯(cuò)誤處理之`foo(...)?`的用法與錯(cuò)誤類型轉(zhuǎn)換小結(jié)

    foo(...)?語法糖為Rust的錯(cuò)誤處理提供了極大的便利,通過結(jié)合map_err方法和From?trait的實(shí)現(xiàn),你可以輕松地處理不同類型的錯(cuò)誤,并保持代碼的簡(jiǎn)潔性和可讀性,這篇文章主要介紹了Rust錯(cuò)誤處理:`foo(...)?`的用法與錯(cuò)誤類型轉(zhuǎn)換,需要的朋友可以參考下
    2024-05-05
  • 詳解Rust中的workspace

    詳解Rust中的workspace

    這篇文章主要向大家介紹Rust中的workspace,主要內(nèi)容包括基礎(chǔ)應(yīng)用、實(shí)用技巧、原理機(jī)制等方面,這個(gè)概念在Rust中是通用的,只不過maven換成了cargo,而模塊變成了crate,下面跟著小編通過一個(gè)例子給大家介紹下
    2022-03-03
  • Rust?use關(guān)鍵字妙用及模塊內(nèi)容拆分方法

    Rust?use關(guān)鍵字妙用及模塊內(nèi)容拆分方法

    這篇文章主要介紹了Rust?use關(guān)鍵字妙用|模塊內(nèi)容拆分,文中還給大家介紹use關(guān)鍵字的習(xí)慣用法,快速引用自定義模塊內(nèi)容或標(biāo)準(zhǔn)庫,以此優(yōu)化代碼書寫,需要的朋友可以參考下
    2022-09-09
  • Rust重載運(yùn)算符之復(fù)數(shù)四則運(yùn)算的實(shí)現(xiàn)

    Rust重載運(yùn)算符之復(fù)數(shù)四則運(yùn)算的實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了Rust如何實(shí)現(xiàn)復(fù)數(shù)以及復(fù)數(shù)的四則運(yùn)算,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-08-08
  • Rust 語言的全鏈路追蹤庫 tracing使用方法

    Rust 語言的全鏈路追蹤庫 tracing使用方法

    這篇文章主要介紹了Rust 語言的全鏈路追蹤庫 tracing,接下來就以 tracing 為例,介紹一下trace 的核心概念以及使用方法,需要的朋友可以參考下
    2022-12-12
  • rust智能指針的具體使用

    rust智能指針的具體使用

    智能指針是一些數(shù)據(jù)結(jié)構(gòu),它們的行為類似于指針但擁有額外的元數(shù)據(jù)和附加功能,本文就來介紹一下rust智能指針的具體使用,感興趣的可以了解一下
    2023-12-12
  • Rust 的 into_owned() 方法實(shí)例詳解

    Rust 的 into_owned() 方法實(shí)例詳解

    into_owned是Rust語言中std::borrow::Cow 枚舉的一個(gè)方法,into_owned確保了調(diào)用者獲得數(shù)據(jù)的獨(dú)立所有權(quán),無論Cow之前是引用還是已經(jīng)擁有數(shù)據(jù),本文給大家介紹Rust 的 into_owned() 方法,感興趣的的朋友跟隨小編一起看看吧
    2024-03-03
  • Rust Option類型基本使用詳解

    Rust Option類型基本使用詳解

    Rust的Option是一種強(qiáng)大的類型,用于處理可能為空的情況,避免了許多空值引起的運(yùn)行時(shí)錯(cuò)誤,本文介紹Rust Option類型詳解,感興趣的朋友一起看看吧
    2024-02-02
  • 最新評(píng)論