Skip to content

Commit 0eea69f

Browse files
committed
// cow1.rs
// // This exercise explores the Cow, or Clone-On-Write type. Cow is a // 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. // // This exercise is meant to show you what to expect when passing data to Cow. // Fix the unit tests by checking for Cow::Owned(_) and Cow::Borrowed(_) at the // TODO markers. // // Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint. // I AM NOT DONE use std::borrow::Cow; fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> { for i in 0..input.len() { let v = input[i]; if v < 0 { input.to_mut()[i] = -v; } } input } #[cfg(test)] mod tests { use super::*; #[test] fn reference_mutation() -> Result<(), &'static str> { let slice = [-1, 0, 1]; let mut input = Cow::from(&slice[..]); match abs_all(&mut input) { Cow::Owned(_) => Ok(()), _ => Err("Expected owned value"), } } #[test] fn reference_no_mutation() -> Result<(), &'static str> { let slice = [0, 1, 2]; let mut input = Cow::from(&slice[..]); match abs_all(&mut input) { Cow::Borrowed(_) => Ok(()), // 无修改,保持借用状态 _ => Err("Expected borrowed value"), } } #[test] fn owned_no_mutation() -> Result<(), &'static str> { let slice = vec![0, 1, 2]; let mut input = Cow::from(slice); match abs_all(&mut input) { Cow::Owned(_) => Ok(()), // 原本就是自有数据,无修改仍保持自有 _ => Err("Expected owned value"), } } #[test] fn owned_mutation() -> Result<(), &'static str> { let slice = vec![-1, 0, 1]; let mut input = Cow::from(slice); match abs_all(&mut input) { Cow::Owned(_) => Ok(()), // 自有数据修改后仍为自有 _ => Err("Expected owned value"), } } }
1 parent 9ea72ed commit 0eea69f

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

exercises/smart_pointers/cow1.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
2020
for i in 0..input.len() {
2121
let v = input[i];
2222
if v < 0 {
23-
// Clones into a vector if not already owned.
2423
input.to_mut()[i] = -v;
2524
}
2625
}
@@ -33,7 +32,6 @@ mod tests {
3332

3433
#[test]
3534
fn reference_mutation() -> Result<(), &'static str> {
36-
// Clone occurs because `input` needs to be mutated.
3735
let slice = [-1, 0, 1];
3836
let mut input = Cow::from(&slice[..]);
3937
match abs_all(&mut input) {
@@ -44,35 +42,31 @@ mod tests {
4442

4543
#[test]
4644
fn reference_no_mutation() -> Result<(), &'static str> {
47-
// No clone occurs because `input` doesn't need to be mutated.
4845
let slice = [0, 1, 2];
4946
let mut input = Cow::from(&slice[..]);
5047
match abs_all(&mut input) {
51-
// TODO
48+
Cow::Borrowed(_) => Ok(()), // 无修改,保持借用状态
49+
_ => Err("Expected borrowed value"),
5250
}
5351
}
5452

5553
#[test]
5654
fn owned_no_mutation() -> Result<(), &'static str> {
57-
// We can also pass `slice` without `&` so Cow owns it directly. In this
58-
// case no mutation occurs and thus also no clone, but the result is
59-
// still owned because it was never borrowed or mutated.
6055
let slice = vec![0, 1, 2];
6156
let mut input = Cow::from(slice);
6257
match abs_all(&mut input) {
63-
// TODO
58+
Cow::Owned(_) => Ok(()), // 原本就是自有数据,无修改仍保持自有
59+
_ => Err("Expected owned value"),
6460
}
6561
}
6662

6763
#[test]
6864
fn owned_mutation() -> Result<(), &'static str> {
69-
// Of course this is also the case if a mutation does occur. In this
70-
// case the call to `to_mut()` returns a reference to the same data as
71-
// before.
7265
let slice = vec![-1, 0, 1];
7366
let mut input = Cow::from(slice);
7467
match abs_all(&mut input) {
75-
// TODO
68+
Cow::Owned(_) => Ok(()), // 自有数据修改后仍为自有
69+
_ => Err("Expected owned value"),
7670
}
7771
}
7872
}

0 commit comments

Comments
 (0)