rustlings

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

cow1.rs (2310B)


      1 // This exercise explores the `Cow` (Clone-On-Write) smart pointer. It can
      2 // enclose and provide immutable access to borrowed data and clone the data
      3 // lazily when mutation or ownership is required. The type is designed to work
      4 // with general borrowed data via the `Borrow` trait.
      5 
      6 use std::borrow::Cow;
      7 
      8 fn abs_all(input: &mut Cow<[i32]>) {
      9     for ind in 0..input.len() {
     10         let value = input[ind];
     11         if value < 0 {
     12             // Clones into a vector if not already owned.
     13             input.to_mut()[ind] = -value;
     14         }
     15     }
     16 }
     17 
     18 fn main() {
     19     // You can optionally experiment here.
     20 }
     21 
     22 #[cfg(test)]
     23 mod tests {
     24     use super::*;
     25 
     26     #[test]
     27     fn reference_mutation() {
     28         // Clone occurs because `input` needs to be mutated.
     29         let vec = vec![-1, 0, 1];
     30         let mut input = Cow::from(&vec);
     31         abs_all(&mut input);
     32         assert!(matches!(input, Cow::Owned(_)));
     33     }
     34 
     35     #[test]
     36     fn reference_no_mutation() {
     37         // No clone occurs because `input` doesn't need to be mutated.
     38         let vec = vec![0, 1, 2];
     39         let mut input = Cow::from(&vec);
     40         abs_all(&mut input);
     41         // TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`.
     42         assert!(matches!(input, todo!()));
     43     }
     44 
     45     #[test]
     46     fn owned_no_mutation() {
     47         // We can also pass `vec` without `&` so `Cow` owns it directly. In this
     48         // case, no mutation occurs (all numbers are already absolute) and thus
     49         // also no clone. But the result is still owned because it was never
     50         // borrowed or mutated.
     51         let vec = vec![0, 1, 2];
     52         let mut input = Cow::from(vec);
     53         abs_all(&mut input);
     54         // TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`.
     55         assert!(matches!(input, todo!()));
     56     }
     57 
     58     #[test]
     59     fn owned_mutation() {
     60         // Of course this is also the case if a mutation does occur (not all
     61         // numbers are absolute). In this case, the call to `to_mut()` in the
     62         // `abs_all` function returns a reference to the same data as before.
     63         let vec = vec![-1, 0, 1];
     64         let mut input = Cow::from(vec);
     65         abs_all(&mut input);
     66         // TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`.
     67         assert!(matches!(input, todo!()));
     68     }
     69 }