rust


Why does an if without an else always result in () as the value?


From this tutorial:
An if without an else always results in () as the value.
Why does Rust impose this restriction and doesn't let an if without an else return other values, like this:
let y = if x == 5 { 10 };
For your example, the right question is: “What would the value of y be if x is not 5?”. What would happen here?
let x = 3;
let y = if x == 5 { 10 };
println!("{}", y); // what?!
You could think that the if-without-else-expression could return an Option<_>, but...
this would mean that the core language depends on yet another library item (those are then called lang items) which everyone tries to avoid
you don't run into this situation too often
you can get the same behavior by adding only a little bit of code (Some() & else { None })
In Rust, nearly everything is an expression (with the exception of let-bindings and expressions ending with a semicolon, so called expression statements). And there are a few examples of expressions always returning (), because nothing else makes sense. These include (compound-)assignments (why?), loops and if-without-else.

Related Links

Having a child modify its parent
How do I access the data sent over UDP?
How to idiomatically copy a slice?
Recursive types and arrays
Can I determine the zero value of generic types?
How to slice a large Vec<i32> as &[u8]?
How to parse i64 from a string? [duplicate]
Why is variable scope dependent on the definition order?
Warning on “unstable” directive
How can I pass a socket as an argument to a function being called within a thread?
error: cannot move out of borrowed content on &mut self
Why does a reference not live long enough in case of “as_slice”?
Getting the error “error: the trait `core::marker::Sized` is not implemented” when trying to return the value from a vector
Traits with associated type in templates
Error handling best-practices
How to call count on an iterator and still use the iterator's items?

Categories

HOME
php
android-espresso
opencart
xcode
paypal-ipn
loopbackjs
whmcs
bing-search
implicit-conversion
repair
scheduler
gtk
cqrs
slide
angular4
corona
coroutine
multiple-tables
jest
kamailio
unordered-multimap
data-collection
tortoisehg
fatfs
region
phpstorm-2016.1
steam-web-api
ms-media-foundation
counting
custom-keyboard
grails-2.5
keyboard-layout
landscape
mv
microkernel
ejml
fiware-wirecloud
google-prediction
destroy
windows-media-player
cloudwatch
word-2016
instafeedjs
nrf51
email-injection
livereload
fuzzer
spring-mongo
nsdata
fragmentstatepageradapter
android-sdcard
verisign
polymerfire
m4
extend
unhandled
kefir.js
scala-swing
flurry-analytics
device-admin
quantify
loginview
neo4jphp
incognito-mode
rails-api
typesafe-stack
apache-shindig
san
google-code-prettify
ax
void-pointers
web-frameworks
aero
session-0-isolation
handshake
linkbutton
xalan
ip-geolocation
android-dialog
n-queens
radscheduler
webresponse
anonymous-types
aptitude
aspbutton
facebook-fbml
data-retrieval
ning

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App