rust


What happens in Rust using “match” if nothing matches?


I'm learning Rust and it looks very interesting. I'm not yet very familiar with "match", but it looks to be fairly integral. I was using the following code (below) to convert String to i64 which included the commented-out line "None" in place of the next line "_". I wondered what happened in the event of a non-match without underscore or whether "None" may be a catch-all. The calling-code requires a positive i64, so negative results in an invalid input (in this case). I'm not sure if it's possible to indicate an error in a return using this example other than perhaps using a struct.
Without underscore as a match item, will "None" catch all, and can it be used instead of underscore?
Is it possible to return an error in a function like this without using a struct as the return value?
In general, is it possible for a non-match using "match" and if so, what happens?
In the example below, is there any difference between using underscore and using "None"?
Example code :
fn fParseI64(sVal: &str) -> i64 {
match from_str::<i64>(sVal) {
Some(iVal) => iVal,
// None => -1
_ => -1
}
}
Without underscore as a match item, will "None" catch all, and can it be used instead of underscore?
In this case, yes. There are only 2 cases for Option<T>: Some<T> or None.
You are already matching the Some case for all values, None is the only case that's left, so
None will behave exactly the same as _.
2. Is it possible to return an error in a function like this without using a struct as the return value?
I'd suggest you read this detailed guide to understand all the possible ways to handle errors -
http://static.rust-lang.org/doc/master/tutorial-conditions.html
Using Options and Results will require you to return a struct.
3. In general, is it possible for a non-match using "match" and if so, what happens?
Rust won't compile a match if it can't guarantee that all the possible cases are handled. You'll need to have a _ clause if you are not matching all possibilities.
4. In the example below, is there any difference between using underscore and using "None"?
No. See Answer #1.
Here are 3 of the many ways you can write your function:
// Returns -1 on wrong input.
fn alt_1(s: &str) -> i64 {
match from_str::<i64>(s) {
Some(i) => if i >= 0 {
i
} else {
-1
},
None => -1
}
}
// Returns None on invalid input.
fn alt_2(s: &str) -> Option<i64> {
match from_str::<i64>(s) {
Some(i) => if i >= 0 {
Some(i)
} else {
None
},
None => None
}
}
// Returns a nice message describing the problem with the input, if any.
fn alt_3(s: &str) -> Result<i64, ~str> {
match from_str::<i64>(s) {
Some(i) => if i >= 0 {
Ok(i)
} else {
Err(~"It's less than 0!")
},
None => Err(~"It's not even a valid integer!")
}
}
fn main() {
println!("{:?}", alt_1("123"));
println!("{:?}", alt_1("-123"));
println!("{:?}", alt_1("abc"));
println!("{:?}", alt_2("123"));
println!("{:?}", alt_2("-123"));
println!("{:?}", alt_2("abc"));
println!("{:?}", alt_3("123"));
println!("{:?}", alt_3("-123"));
println!("{:?}", alt_3("abc"));
}
Output:
123i64
-1i64
-1i64
Some(123i64)
None
None
Ok(123i64)
Err(~"It's less than 0!")
Err(~"It's not even a valid integer!")

Related Links

Posting form data with nickel.rs works the first time, returns 404 subsequent times
How do I hold a collection of one struct in another where lifetimes are not predictable?
Why does Rust have both call by value and call by reference?
Error: borrowed value is only… reference must be valid for
In Rust how do you pass a function as a parameter?
Obtaining rustdoc for particular function or trait
Rust slice ownership outlives its scope
What's purpose of ErrorKind::__Nonexhaustive?
A set type that preserves insertion order
Rust closure type mismatches of seemingly opposing natures
How can I build multiple binaries with Cargo?
borrowed value does not live long enough in this case ( Vec<&Fn(i32) -> i32> )
Mutability issue with self and collections in Rust
Recommended way to wrap C lib initialization/destruction routine
Return either a borrowed or owned type in Rust
Is there some way to not show a warning for non snake case identifiers?

Categories

HOME
hpoo
google-chrome
jboss
blast
jms
enums
hid
vsts-build
yql
google-webmaster-tools
swift2
apache-kafka-connect
mosquitto
azure-mobile-services
u-boot
polymer-2.x
selection
gtk
siesta
spring-amqp
pm2
createjs
smartgwt
stimulsoft
amazon-elb
xbox360
google-tasks-api
l20n
playframework-2.0
string-formatting
conda
pyopencl
high-availability
strophe.js
amazon-cloudtrail
jsonresult
d3.js-v4
instructions
vcenter
riotjs
seafile-server
gnu-classpath
cups
code-rally
installanywhere
google-now
grunt-contrib-watch
optionaldataexception
xcode8.2
audio-converter
nusoap
hibernate-ogm
.when
fitbit
cefpython
okio
raw-sockets
coovachilli
odp.net-managed
onepage-checkout
odesk
dsa
whitespace-language
mutators
famo.us
password-recovery
rebar
storing-data
hyperloglog
reference-counting
gpu-programming
lsa
eclim
progress-db
acitree
iirf
django-facebook
jacob
web-frameworks
arel
gprof
story
live-wallpaper
msinfo32
word-frequency
servercontrol
photo-management
webresponse
ccl
raw-data
data-retrieval

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