Pub FN unwrap_or (self, default: T) T Returns the contained Some value or a provided default. Arguments passed to unwrap_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use unwrap_or_else, which is lazily evaluated.
Returns the contained Some value or computes it from a closure. Pub FN map (self, f: F) Option where F: Nonce (T) U, Maps an Option
Pub FN map_or (self, default: U, f: F) U where F: Nonce (T) U, Applies a function to the contained value (if any), or returns the provided default (if not). Arguments passed to map_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use map_or_else, which is lazily evaluated.
Pub FN map_or_else (self, default: D, f: F) U where D: Nonce () U, F: Nonce (T) U, Applies a function to the contained value (if any), or computes a default (if not). Pub FN ok_or
Pub FN ok_or_else
Pub FN filter
(self, predicate: P) Option
This function works similar to Iterator::filter(). You can imagine the Option
Filter() lets you decide which elements to keep. Arguments passed to or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use or_else, which is lazily evaluated.
Inserts value into the option if it is None, then returns a mutable reference to the contained value. Inserts a value computed from f into the option if it is None, then returns a mutable reference to the contained value.
Takes the value out of the option, leaving a None in its place. Pub FN replace (smut self, value: T) Option
Pub FN zip_with (self, other: Option , f: F) Option
Consumes self while expecting None and returning nothing. Panics if the value is a Some, with a panic message including the passed message, and the content of the Some.
Consumes self while expecting None and returning nothing. Panics if the value is a Some, with a custom panic message provided by the Some's value.
Converts a string to an integer, turning poorly-formed strings into 0 (the default value for integers). Parse converts a string to any other type that implements Frost, returning None on error.
Leaves the original Option in-place, creating a new one with a reference to the original one, additionally coercing the contents via Dark. Leaves the original Option in-place, creating a new one containing a mutable reference to the inner type's Dark::Target type.
Flattening once only removes one level of nesting: The map method takes the self argument by value, consuming the original, so this technique uses as_ref to first take an Option to a reference to the value inside the original.
Should no None occur, a container with the values of each Option is returned. Here is an example which increments every integer in a vector.
We use the checked variant of add that returns None when the calculation would result in an overflow. As you can see, this will return the expected, valid items.
Here is another example that tries to subtract one from another list of integers, this time checking for underflow: Here is a variation on the previous example, showing that no further elements are taken from inter after the first None.
#FN clamp (self, min: Self, max: Self) Self This is a nightly-only experimental API. Restrict a value to a certain interval.
A return of Err(e) means that execution should branch to the innermost enclosing catch, or return from the function. Wrap an OK value to construct the composite result.
Hide detailsView details born merged commit 4fd4a98 into rust slang:master Dec 8, 2020 11 checks passed So I just got an M1 mac, and I've been kicking the tires on the developer experience.
Even big, widely-used tools like Neo vim and NodeJS either don't work, have to be built manually from source, or installed and run using x86 emulation. Kudos to the rust development team and community for delivering on promise.
Rust avoids the billion dollar mistake of including null s in the language. There is plenty of material out there detailing why an Option type is better than null, so I won’t go too much into that.
So, unwrap() panics and exits the program when the Option is empty i.e. None. Since Option is actually just an ENIM, we can use pattern matching to print the middle name if it is present, or a default message if it is not.
Turns out we can conveniently use ref in a pattern match to borrow a reference. Again, recalling that string can be coerced to STR, this solves our type mismatch problem.
Instead of first using map() to transform to another Option and then unwrapping it, we can use the convenience method map_or() which allows us to do this in one call: And_then() is another method that allows you to compose Options (equivalent to flat map in other languages).
As such, in the case of Jon, since the middle name is None, the get_nickname() function will not be called at all, and the above will print “(none found)”.