2018-02-22 09:09:53 +03:00
|
|
|
// option1.rs
|
2016-02-11 03:37:26 +03: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`!
|
2019-11-11 18:51:38 +03:00
|
|
|
// Execute `rustlings hint option1` for hints :)
|
2016-02-11 03:37:26 +03:00
|
|
|
|
2019-11-11 15:38:24 +03:00
|
|
|
// I AM NOT DONE
|
|
|
|
|
2019-07-27 02:44:10 +03:00
|
|
|
pub fn pop_too_much() -> bool {
|
2016-02-11 03:37:26 +03: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();
|
2019-05-22 14:48:32 +03:00
|
|
|
println!(
|
|
|
|
"The second-to-last item in the list is {:?}",
|
|
|
|
second_to_last
|
|
|
|
);
|
2019-07-27 02:44:10 +03:00
|
|
|
true
|
2016-02-11 03:37:26 +03:00
|
|
|
}
|
|
|
|
|
2019-07-27 02:44:10 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2016-02-11 03:37:26 +03:00
|
|
|
|
2019-07-27 02:44:10 +03:00
|
|
|
#[test]
|
|
|
|
fn should_not_panic() {
|
2019-10-26 00:27:24 +03:00
|
|
|
assert!(pop_too_much());
|
2019-07-27 02:44:10 +03:00
|
|
|
}
|
|
|
|
}
|