In Swift, !
means to unwrap an optional (possible value).
means to unwrap an optional (possible value). rust.
Please review Appendix B: Operators and Symbols of The Rust Programming Language. In this case, the double colon ( :: ) is the path separator. Paths are comprised of crates, modules, and items.
The logical complement operator, also known as the NOT operator in Java, is represented by an exclamation mark '! '. This operator changes true values to false and false to true. This operator only works with boolean .
In Rust, the println! is a macro defined in the std::fmt. It is used to handle printing dynamic information to the user. The println! is closely similar to the print! but appends a new line.
println!
is not a function, it is a macro. Macros use !
to distinguish them from normal method calls. The documentation contains more information.
See also:
Rust uses the Option
type to denote optional data. It has an unwrap
method.
Rust 1.13 added the question mark operator ?
as an analog of the try!
macro (originally proposed via RFC 243).
An excellent explanation of the question mark operator is in The Rust Programming Language.
fn foo() -> Result<i32, Error> { Ok(4) } fn bar() -> Result<i32, Error> { let a = foo()?; Ok(a + 4) }
The question mark operator also extends to Option
, so you may see it used to unwrap a value or return None
from the function. This is different from just unwrapping as the program will not panic:
fn foo() -> Option<i32> { None } fn bar() -> Option<i32> { let a = foo()?; Some(a + 4) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With