Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the println! function use an exclamation mark in Rust?

Tags:

rust

In Swift, ! means to unwrap an optional (possible value).

like image 909
Chéyo Avatar asked Apr 13 '15 17:04

Chéyo


People also ask

What is rust exclamation?

means to unwrap an optional (possible value). rust.

What does the :: mean in 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.

What is exclamation mark used for in Java?

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 .

What is Println in Rust?

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.


1 Answers

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:

  • What is the difference between macros and functions in Rust?

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) } 
like image 132
Shepmaster Avatar answered Oct 09 '22 12:10

Shepmaster