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
Ternary conditional operator
(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=== Being an [[expression-oriented programming language]], Rust's existing <code>if ''expr<sub>1</sub>'' else ''expr<sub>2</sub>''</code> syntax can behave as the traditional {{code|?:}} ternary operator does. Earlier versions of the language did have the {{code|?:}} operator but it was removed<ref>{{Cite web|url=https://github.com/rust-lang/rust/pull/1705|title = Remove Ternary Operator by pwoolcoc 路 Pull Request #1705 路 rust-lang/Rust|website = [[GitHub]]}}</ref> due to duplication with {{code|if}}.<ref>{{Cite web|url=https://github.com/rust-lang/rust/issues/1698|title=Remove ternary operator 路 Issue #1698 路 rust-lang/Rust|website=[[GitHub]]}}</ref> Note the lack of semi-colons in the code below compared to a more declarative {{code|if}}...{{code|else}} block, and the semi-colon at the end of the assignment to {{code|y}}. <syntaxhighlight lang="rust"> let x = 5; let y = if x == 5 { 10 } else { 15 }; </syntaxhighlight> This could also be written as: <syntaxhighlight lang="rust"> let y = if x == 5 { 10 } else { 15 }; </syntaxhighlight> Note that curly braces are mandatory in Rust conditional expressions. You could also use a {{code|match}} expression: <syntaxhighlight lang="rust"> let y = match x { 5 => 10, _ => 15, }; </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)