In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the concept of Result/Option/unwrap/". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the concept of Result/Option/unwrap/"?
1. Option-nullable variable
Although there is the concept of null in Rust, using null is not a common pattern in Rust. Suppose we want to write a function and enter the name of a mobile operating system, and this function will return the name of its app store. If the string iOS is passed in, the function returns App Store;. If the string android is passed in, the function returns Play Store. Any other input is considered invalid.
In most development languages, we can choose to return null or the string invalid to represent invalid results, but this is not the use of Rust.
Authentic Rust code should let the function return an Option. Option or rather Option is a generic type, which can be Some or None (for ease of reading, the type parameter T will be omitted in subsequent articles). Rust calls Some and None Variant-a concept that doesn't exist in other languages, so I'm not going to define what a variant is.
In our example, the function would normally return the string constant App Store or Play Store wrapped in the Some variant. In abnormal cases, the function returns None.
Fn find_store (mobile_os: & str)-> Option {match mobile_os {"iOS" = > Some ("App Store"), "android" = > Some ("Play Store"), _ = > None}}
To use find_store (), we can call it as follows:
Fn main () {println! ("{}", match find_store ("windows") {Some (s) = > s, None = > "Not a valid mobile OS"});}
The complete code is as follows:
Fn find_store (mobile_os: & str)-> Option {match mobile_os {"iOS" = > Some ("App Store"), "android" = > Some ("Play Store"), _ = > None}} fn main () {println! ("{}", match find_store ("windows") {Some (s) = > s, None = > "Not a valid mobile OS"}) } 2. Result-result containing error message
Result, or rather Result, is a concept related to Option in Rust, which is an enhanced version of Option.
Result may have one of the following results:
Ok (T): the result is member T
Err (E): the result is fault member E
Unlike what we saw earlier that Option can contain Some or None, Result contains error-related information that is not available in Option.
Let's look at a function instance that returns a Result. This function is extracted from the serde_json library used to parse JSON strings and is signed as follows:
Pub fn from_str Result where T: Deserialize
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.