rustlings/exercises/error_handling/option1.rs

32 lines
737 B
Rust
Raw Normal View History

2018-02-22 13:09:53 +07:00
// option1.rs
2016-02-11 06:37:26 +06:00
// This example panics because the second time it calls `pop`, the `vec`
// is empty, so `pop` returns `None`, and `unwrap` panics if it's called
// on `None`. Handle this in a more graceful way than calling `unwrap`!
// Execute `rustlings hint option1` for hints :)
2016-02-11 06:37:26 +06:00
// I AM NOT DONE
pub fn pop_too_much() -> bool {
2016-02-11 06:37:26 +06:00
let mut list = vec![3];
let last = list.pop().unwrap();
println!("The last item in the list is {:?}", last);
let second_to_last = list.pop().unwrap();
println!(
"The second-to-last item in the list is {:?}",
second_to_last
);
true
2016-02-11 06:37:26 +06:00
}
#[cfg(test)]
mod tests {
use super::*;
2016-02-11 06:37:26 +06:00
#[test]
fn should_not_panic() {
assert!(pop_too_much());
}
}