mirror of
https://github.com/notohh/rustlings.git
synced 2024-12-23 11:48:09 -05:00
9bdb0a12e4
Hints are now accessible using the CLI subcommand `rustlings hint <exercise name`. BREAKING CHANGE: This fundamentally changes the way people interact with exercises.
29 lines
719 B
Rust
29 lines
719 B
Rust
// option1.rs
|
|
// 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 :)
|
|
|
|
pub fn pop_too_much() -> bool {
|
|
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
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn should_not_panic() {
|
|
assert!(pop_too_much());
|
|
}
|
|
}
|