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 :: rust•armanriazi•concept•semantic 
Rust :: How to pass a string literal 
Rust :: Repeat the given string exactly n times 
Rust :: rust•armanriazi•type•wraper•mutable 
Rust :: armanriazi•rust•concept•dynamic•dispatch 
Rust :: sort_by in rust 
Rust :: armanriazi•rust•union 
Rust :: key value in for loop rust 
Rust :: armanriazi•rust•unsafe•extern•mangling 
Rust :: loop label in rust 
Rust :: armanriazi•rust•error•E0605•non-primitive cast 
Rust :: rust the size for values of type `str` cannot be known at compilation time the trait `Sized` is not implemented for `str` 
Rust :: armanriazi•rust•trait•blanket 
Rust :: armanriazi•rust•concept•jargon 
Lua :: roblox check if player has gamepass 
Lua :: repeating loop roblox 
Lua :: lua round number 
Lua :: how to teleport all players in roblox studio 
Lua :: roblox table.find() 
Lua :: lua gsub 
Lua :: roblox destroy game script 
Lua :: lua clear table 
Lua :: hello world in lua 
Lua :: roblox lua how to apply gravity to a object 
Lua :: int and float in lua 
Lua :: when do true loop on roblox 
Matlab :: matlab read from txt file 
Matlab :: matlab plotting multiple lines on one graph 
Matlab :: matlab new line in string 
Basic :: how to remove characters from the end of a string visual basic 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =