![]() There are also a bunch of ways to do things to an Option without checking whether it has a value or not. If let actually works with any enumerated type! Using map Discover popular ORMs used in the TypeScript landscapeįor instance, the following code will print "Got " if t has a value, and do nothing if t is None: // t is an Option.Explore Tauri, a new framework for building binaries.Learn how to animate your React app with AnimXYZ.Switch between multiple versions of Node.Use React's useEffect to optimize your application's performance.Don't miss a moment with The Replay, a curated newsletter from LogRocket.It’s pretty common to want to do something only if an Option has a real value, and if let is a concise way to combine doing that with getting the underlying value. _ => println!("OK not handling this case.") Some(x) => println!("Got value ", x), // the other match arm None => println!("No value here!"), // one match arm This works on any enumerated type, and looks like this: // t is an Option The most basic way to see whether an Option has a value or not is to use pattern matching with a match expression. If you want, you can check whether the Option has a value before calling unwrap() like this: // t is an Optionīut, there are more concise ways to do this (for instance, using if let, which we’ll cover later). There’s also an unwrap_or(), which lets you specify a default if the value is None, so Some(5).unwrap_or(7) is 5 and None.unwrap_or(7) is 7. The only difference is that expect() lets you specify a custom message that prints out to the console as the program exits. (This is known as panicking, and there are cases when it is recoverable, but for simplicity, we’ll gloss over that here.) If you’re sure that an Option has a real value inside, then expect() and unwrap() are for you! They return the value inside, but if the variable is actually None, your program exits. But, Rust has a lot of tricks up its sleeve to help! Using expect and unwrap The downside is that this tends to make code irritatingly verbose. This avoids problems in other languages that don’t have nullable types.įor example, in C++, std::find() returns an iterator, but you must remember to check it to make sure it isn’t the container’s end()-if you forget this check and try to get the item out of the container, you get undefined behavior. One of the benefits of an API that returns an Option is that to get the value inside, callers are forced to check if the value is None or not. It has a pop() method that returns an Option, which will be None if the vector is empty or Some(value) containing the last value of the vector. For example, Vec is Rust’s type that represents a vector (or variable-sized array). This is where value can be any value of type T. It’s an enumerated type (also known as algebraic data types in some other languages) where every instance is either: Rust’s version of a nullable type is the Option type. Using expect, unwrap, match, and if let.So, in this post we’ll cover the following: One of these conveniences is using enums, specifically the Option and Result types. Rust is a systems programming language that focuses on safety and performance, and has been voted the “most loved language” on Stack Overflow’s annual survey for six years running! One of the reasons Rust is such a joy to program in is that, despite its focus on performance, it has a lot of well-thought-out conveniences that are frequently associated with higher-level languages. He enjoys working on projects in his spare time and enjoys writing about them! Understanding Rust Option and Result enums Of the blog, you can find links to all posts in the series at the top of theįeed.Greg Stoll Follow Greg is a software engineer with over 20 years of experience in the industry. This post is part of a series based on teaching Rust at FPĬomplete. If you're interested, please check out the Rust Crash Course eBook.īelow are the solutions to the exercises from the last Rust CrashĬourse lesson, "Rule of Three - Parameters, Iterators, and Closures." I'd recommend reading that version instead of these posts. Heads up This blog post series has been updated and published as an eBook by FP Complete.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |