summaryrefslogtreecommitdiff
path: root/rustlings/solutions/19_smart_pointers/cow1.rs
diff options
context:
space:
mode:
authorHunter Kvalevog <hunter@kvog.sh>2026-09-27 18:04:07 -0500
committerHunter Kvalevog <hunter@kvog.sh>2026-09-27 18:04:07 -0500
commit472f99c8011b8de3f796c1ddc5d8f50164a34940 (patch)
tree49b14da76ada52943ce4fd147c2aa3fe50d05d1a /rustlings/solutions/19_smart_pointers/cow1.rs
parent67fb6c0eb507965836201551f90c7d637bf6cee0 (diff)
rustlings: complete 19
Diffstat (limited to 'rustlings/solutions/19_smart_pointers/cow1.rs')
-rw-r--r--rustlings/solutions/19_smart_pointers/cow1.rs69
1 files changed, 67 insertions, 2 deletions
diff --git a/rustlings/solutions/19_smart_pointers/cow1.rs b/rustlings/solutions/19_smart_pointers/cow1.rs
index dcf2377..461143b 100644
--- a/rustlings/solutions/19_smart_pointers/cow1.rs
+++ b/rustlings/solutions/19_smart_pointers/cow1.rs
@@ -1,4 +1,69 @@
+// This exercise explores the `Cow` (Clone-On-Write) smart pointer. It can
+// enclose and provide immutable access to borrowed data and clone the data
+// lazily when mutation or ownership is required. The type is designed to work
+// with general borrowed data via the `Borrow` trait.
+
+use std::borrow::Cow;
+
+fn abs_all(input: &mut Cow<[i32]>) {
+ for ind in 0..input.len() {
+ let value = input[ind];
+ if value < 0 {
+ // Clones into a vector if not already owned.
+ input.to_mut()[ind] = -value;
+ }
+ }
+}
+
fn main() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference_mutation() {
+ // Clone occurs because `input` needs to be mutated.
+ let vec = vec![-1, 0, 1];
+ let mut input = Cow::from(&vec);
+ abs_all(&mut input);
+ assert!(matches!(input, Cow::Owned(_)));
+ }
+
+ #[test]
+ fn reference_no_mutation() {
+ // No clone occurs because `input` doesn't need to be mutated.
+ let vec = vec![0, 1, 2];
+ let mut input = Cow::from(&vec);
+ abs_all(&mut input);
+ assert!(matches!(input, Cow::Borrowed(_)));
+ // ^^^^^^^^^^^^^^^^
+ }
+
+ #[test]
+ fn owned_no_mutation() {
+ // We can also pass `vec` without `&` so `Cow` owns it directly. In this
+ // case, no mutation occurs (all numbers are already absolute) and thus
+ // also no clone. But the result is still owned because it was never
+ // borrowed or mutated.
+ let vec = vec![0, 1, 2];
+ let mut input = Cow::from(vec);
+ abs_all(&mut input);
+ assert!(matches!(input, Cow::Owned(_)));
+ // ^^^^^^^^^^^^^
+ }
+
+ #[test]
+ fn owned_mutation() {
+ // Of course this is also the case if a mutation does occur (not all
+ // numbers are absolute). In this case, the call to `to_mut()` in the
+ // `abs_all` function returns a reference to the same data as before.
+ let vec = vec![-1, 0, 1];
+ let mut input = Cow::from(vec);
+ abs_all(&mut input);
+ assert!(matches!(input, Cow::Owned(_)));
+ // ^^^^^^^^^^^^^
+ }
}