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 :: split text on multiple separators and put into a list 
Rust :: rust match enum 
Lua :: roblox check if player has gamepass 
Lua :: roblox studio teleport on collision 
Lua :: roblox rainbow part 
Lua :: repeating loop roblox 
Lua :: lowercase lua 
Lua :: how to make a part rotate roblox 
Lua :: roblox difference between __index and __newindex 
Lua :: how to teleport all players in roblox studio 
Lua :: lua drawinrect 
Lua :: how to exit current scope roblox 
Lua :: lua printing 
Lua :: error: LINK : fatal error LNK1561: entry point must be defined 
Lua :: roblox number generator 
Lua :: lua clear table 
Lua :: lua convert function to string 
Lua :: roblox studio pause physics 
Lua :: pcall lua 
Lua :: unsur unsur hidrogen 
Lua :: how to add a damage decimal in roblox studio 
Lua :: FiveM how to check where nearest player is 
Matlab :: matlab poly 
Matlab :: matlab stop running function 
Basic :: git token 
Basic :: como colocar aspas duplas em vbnet 
Elixir :: elixir list map key string to atom 
Elixir :: elixir nested if 
Scala :: scala yield how to share one loop 
Actionscript :: add to github 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =