-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: exercises to manage of None and Errors
- Use of Result and Option - Use of Some and None - Use of Ok and Err - Use of match - Use of unwrap
- Loading branch information
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
**/*.rs.bk |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "match-some-err-options-results" | ||
version = "0.1.0" | ||
authors = ["Stivenson Rincón <stivenson.rpm@gmail.com>"] | ||
edition = "2018" | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Return result or None | ||
fn get_slice_or_possible_none(phrase: String) -> Option<String> { | ||
if &phrase == "" { | ||
None | ||
} else { | ||
let first_letters = &phrase[0..2]; | ||
Some(first_letters.to_string()) | ||
} | ||
} | ||
|
||
// Return result or error | ||
fn get_div_or_possible_error(operators: (u32, u32)) -> Result<u32, &'static str>{ | ||
if operators.1 == 0 { | ||
Err("Second operator is zero") | ||
} else { | ||
Ok(operators.0 / operators.1) | ||
} | ||
} | ||
|
||
fn main() { | ||
|
||
// Omit check of possible None | ||
println!("First Letters of Empty string: {:?}",get_slice_or_possible_none("hi! Stivenson".to_string()).unwrap()); | ||
println!("First Letters of string: {:?}",get_slice_or_possible_none("".to_string())); | ||
|
||
|
||
|
||
// Manage possible "None" or successful | ||
let res = get_slice_or_possible_none("hi! Stivenson".to_string()); | ||
match res { | ||
Some(letters) => { | ||
println!("First Letters of string: {:?}", letters); | ||
}, | ||
None => { | ||
println!("Empty String"); | ||
} | ||
} | ||
let res = get_slice_or_possible_none("".to_string()); // overwriting to res | ||
match res { | ||
Some(letters) => { | ||
println!("First Letters of string: {:?}", letters); | ||
}, | ||
None => { | ||
println!("Empty String"); | ||
} | ||
} | ||
|
||
|
||
// Manage possible error or successful | ||
let possible_error = get_div_or_possible_error((10,2)); | ||
match possible_error { | ||
Ok(result_div) => { | ||
println!("Div without error: {:?}", result_div); | ||
}, | ||
Err(message) => { | ||
println!("Div with error: {:?}", message); | ||
} | ||
} | ||
let possible_error = get_div_or_possible_error((10,0)); // overwriting to possible_error | ||
match possible_error { | ||
Ok(result_div) => { | ||
println!("Div without error: {:?}", result_div); | ||
}, | ||
Err(message) => { | ||
println!("Div with error: {:?}", message); | ||
} | ||
} | ||
} |