Search
 
SCRIPT & CODE EXAMPLE
 

RUST

armanriazi•rust•t•opt•?

The error message also mentioned that ? can be used with Option<T> values as well. As with using ? on Result, you can only use ? on Option in a function that returns an Option. The behavior of the ? operator when called on an Option<T> is similar to its behavior when called on a Result<T, E>: if the value is None, the None will be returned early from the function at that point. If the value is Some, the value inside the Some is the resulting value of the expression and the function continues
This pattern of propagating errors is so common in Rust that Rust provides the question mark operator ? to make this easier.
Sample : propagating errors
    match f.read_to_string(&mut s) {
        Ok(_) => Ok(s),
        Err(e) => Err(e),
    }

what the ? operator does: error values that have the ? operator called on them go through the from function, defined in the From trait in the standard library, which is used to convert errors from one type into another. When the ? operator calls the from function, the error type received is converted into the error type defined in the return type of the current function. This is useful when a function returns one error type to represent all the ways a function might fail, even if parts might fail for many different reasons. As long as there’s an impl From<OtherError> for ReturnedError to define the conversion in the trait’s from function, the ? operator takes care of calling the from function automatically.

In the context of Listing 9-7, the ? at the end of the File::open call will return the value inside an Ok to the variable f. If an error occurs, the ? operator will return early out of the whole function and give any Err value to the calling code. The same thing applies to the ? at the end of the read_to_string call.

The ? operator eliminates a lot of boilerplate and makes this function’s implementation simpler. We could even shorten this code further by chaining method calls immediately after the ?

{either option&result :
you can’t mix and match. The ? operator won’t automatically convert a Result to an Option or vice versa; in those cases, there are methods like the ok method on Result or the ok_or method on Option that will do the conversion explicitly.
}
Comment

armanriazi•rust•static•vs•cons

It can be confusing whether or not you should use a constant item or a static item. Constants should, in general, be preferred over statics unless one of the following are true:

Large amounts of data are being stored
The single-address property of statics is required.
Interior mutability is required.
{
Another difference between constants and static variables is that static variables can be mutable.
Accessing and modifying mutable static variables is unsafe
}
Comment

armanriazi•rust•static

All access to a static is safe, but there are a number of restrictions on statics:

The type must have the Sync trait bound to allow thread-safe access.
Constants cannot refer to statics.
Comment

PREVIOUS NEXT
Code Example
Rust :: armanriazi•rust•generic•monomorphization 
Rust :: rust missing lifetime specifier 
Rust :: armanriazi•rust•rc•vs•arc 
Rust :: set interval Rust 
Rust :: rust iterate over split 
Rust :: rust return the result with trait exit status 
Rust :: rust•armanriazi•loop•listen 
Rust :: rmarmanriazi•rust•concept•polymorphism 
Rust :: find prime numbers with the sieve of Eratosthenes 
Rust :: update a value in hashmap in rust 
Rust :: armanriazi•rust•error•E0277•the trait bound `` is not satisfied 
Rust :: rust•armanriazi•concept•nan 
Lua :: print table lua 
Lua :: base64 decode lua 
Lua :: kill player when something touchd lua 
Lua :: roblox on touch script 
Lua :: lua globals 
Lua :: keywords in lua 
Lua :: roblox tweenservice 
Lua :: repeat until in lua 
Lua :: table.find lua 
Lua :: How to split license id fivem 
Lua :: minetest lua delay 
Lua :: pico8 draw sprite 
Lua :: lua compare time 
Matlab :: switch matlab 
Matlab :: octave wait 
Basic :: mongodb command remove by _id 
Basic :: dos assign command output to variable (when output is of multiple lines) 
Elixir :: elixir read csv file 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =