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

In Rust, how can I fix the error “the trait `core::kinds::Sized` is not implemented for the type `Object+'a`”
Boxed value does not live long enough
inspect rust traits at runtime
Enums with constructions in Rust
How can I unify the lifetimes between a struct and a trait?
PNaCl support for Rust
Store value in struct through trait
Rust not running helloworld example
Why does #[derive(Show)] not work anymore?
How do lifetime bounds on structs work in Rust?
Rust - how to retrieve a user-defined type from for loop
rust: error: macro undefined: 'fail!'
Match String Tuple in Rust
How can I make a structure with internal references?
Iterating over the contents of an Option, or over a specific value
Why do executables have a dependency on glibc?

Categories

HOME
user-interface
knockout.js
apache-nifi
weblogic12c
windows-8.1
cucumber
d3.js
concurrency
braintree
nsis
mathematical-optimization
cypher
vue-resource
mosquitto
hyperledger
visualforce
native-base
cisco
lapack
responsivevoice
google-awareness
synthesis
varnish-vcl
tfsbuild
http-authentication
spyder
yii2-basic-app
sqlplus
neo4j.rb
coreldraw
acl
substring
sipp
pdf.js
python-appium
offline-caching
sap-lumira
android-service
points
echosign
tortoisehg
sparkle
splunk-query
copy-protection
john-the-ripper
private-key
kendo-listview
des
jade4j
hidden-markov-models
tidal-scheduler
assemblies
get-event-store
pyttsx
word-cloud
robust
virtual-memory
bit-shift
adblock
viewmodel
windows-media-player
directwrite
scala-breeze
cordova-ios
renderman
exc-bad-access
android-mediarecorder
yargs
ternary-operator
zynq
conan
bbc-micro
apache-pig-grunt
frame-grab
okta-api
currency-exchange-rates
pack
arbre
self
knife
360-degrees
dnsjava
flurry-analytics
dc
angular-ui-select
neo4jphp
pre
pbkdf2
unrealscript
stty
libxml-js
examine
icenium
tridion-2011
struts2-json-plugin
opends
nsfont
exponent
wiktionary
css-friendly
nsconnection
downgrade
mygeneration
aggregator
fgetc
rakudo
server-load

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App