Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Conditional (computer programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Rust==== {{Main|Rust (programming language)}} In [[Rust (programming language)|Rust]], <code>if</code> is always an expression. It evaluates to the value of whichever branch is executed, or to the unit type <code>()</code> if no branch is executed. If a branch does not provide a return value, it evaluates to <code>()</code> by default. To ensure the <code>if</code> expression's type is known at compile time, each branch must evaluate to a value of the same type. For this reason, an <code>else</code> branch is effectively compulsory unless the other branches evaluate to <code>()</code>, because an <code>if</code> without an <code>else</code> can always evaluate to <code>()</code> by default.<ref>{{cite web|title=If and if let expressions|url=https://doc.rust-lang.org/reference/expressions/if-expr.html|access-date=November 1, 2020}}</ref> <syntaxhighlight lang="rust"> // Assign my_variable some value, depending on the value of x let my_variable = if x > 20 { 1 } else { 2 }; // This variant will not compile because 1 and () have different types let my_variable = if x > 20 { 1 }; // Values can be omitted when not needed if x > 20 { println!("x is greater than 20"); } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)