rustlings

solving rustlings ft. dracuxan
git clone [email protected]:dracuxan/rustlings.git
Log | Files | Refs

options2.rs (796B)


      1 fn main() {
      2     // You can optionally experiment here.
      3 }
      4 
      5 #[cfg(test)]
      6 mod tests {
      7     #[test]
      8     fn simple_option() {
      9         let target = "rustlings";
     10         let optional_target = Some(target);
     11 
     12         // if-let
     13         if let Some(word) = optional_target {
     14             assert_eq!(word, target);
     15         }
     16     }
     17 
     18     #[test]
     19     fn layered_option() {
     20         let range = 10;
     21         let mut optional_integers: Vec<Option<i8>> = vec![None];
     22 
     23         for i in 1..=range {
     24             optional_integers.push(Some(i));
     25         }
     26 
     27         let mut cursor = range;
     28 
     29         // while-let with nested pattern matching
     30         while let Some(Some(integer)) = optional_integers.pop() {
     31             assert_eq!(integer, cursor);
     32             cursor -= 1;
     33         }
     34 
     35         assert_eq!(cursor, 0);
     36     }
     37 }