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

How to get the name of current program without the directory part?
Changing key of HashMap from child method
Cannot build a delete function for a binary search tree in Rust because the borrowed value does not live long enough
Why does AtomicUsize not implement Send?
How can I define a list of functions to call? [duplicate]
How can I tell if a Rust library is deprecated?
error: non-scalar cast: `core::option::Option<i32>` as `usize`
Lifetime of variable in map/flat_map in Rust
Can associated constants be used to initialize fixed size arrays?
Why do Arc and Mutex allow me to change the value of an immutable variable?
Unable to download a package via cargo — timeout
“borrowed value does not live long enough” seems to blame the wrong thing
Is the destructor of Arc guaranteed to be called before upgrading a Weak reference will return None?
Can't find crate for `rayon`
How to write a function that returns Vec<Path>?
How to implement a long running process with progress in Rust, available via a Rest api?

Categories

HOME
amazon-web-services
adfs3.0
key
compilation
subdomain
biztalk
dronekit-python
openflow
azure-data-lake
impala
sentry
firebase-dynamic-links
kurento
polymer-2.x
slide
csrf
vimeo
angular4
feature-extraction
outlook-restapi
spring-jpa
synthesis
scala-ide
boomi
significance
wsf
android-custom-view
dashdb
backtracking
websphere-portal
web-push
data-collection
crud
msdeploy
laravel-4.2
boolean-logic
instructions
series
fotorama
john-the-ripper
caesar-cipher
knights-tour
apptentive
android-canvas
express-session
widestring
python-sounddevice
newtons-method
dagger
transparency
zend-form
autorelease
supertest
non-linear-regression
phppgadmin
android-webservice
watir-webdriver
test-data
degrees
google-shopping-api
annotatorjs
thread-exceptions
nsrunloop
carmen
jenkins-workflow
right-to-left
swiffy
verisign
teensy
servicestack-bsd
ambiguity
asp.net-web-api-helppages
android-studio-import
angular-amd
ssmtp
mod-auth-openidc
tkx
flurry-analytics
rebar
image-rotation
smacss
sql-function
lync-2010
spring-security-acl
frontbase
boinc
mp4parser
chartfx
jquery-lazyload
red-system
system-requirements
coordinate
websphere-6.1
mmc3
litespeed
haiku
fragment-identifier
mygeneration
explicit
3-tier
uninstaller
fgetc
resharper-5.0
facebook-fbml
firephp
downcasting
jboss-mdb

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