Compare commits

...

7 Commits
perro ... main

Author SHA1 Message Date
liv 6f0e5d13ed
Merge pull request #1372 from rust-lang/all-contributors/add-black-puppydog
docs: add black-puppydog as a contributor for content
2023-02-15 14:17:07 +01:00
allcontributors[bot] 046a18cd16
docs: update .all-contributorsrc [skip ci] 2023-02-15 13:16:59 +00:00
allcontributors[bot] e1e67d0d41
docs: update AUTHORS.md [skip ci] 2023-02-15 13:16:58 +00:00
liv 216d08d013
Merge pull request #1244 from black-puppydog/cow-rework
refactor(cow1):  replace main with tests
2023-02-15 14:16:22 +01:00
Daan Wynen bbdc5c6039 refactor(cow1): replace main with tests
Following the discussion in #1195 this is the best I could come up with.
The issue for me (and apparently a few other learners) was that the code
needed to complete the exercise was not _missing_, but was rather there
but wrong.

In the end, what made the difference between this exercise and others
(for me) was that in this exercise I was supposed to learn what to
*expect* of an output. So I think it makes sense here to let the learner
modify the tests and not the code itself.

Fixes #1195

Signed-off-by: Daan Wynen <black.puppydog@gmx.de>

# Conflicts:
#	info.toml
2023-02-14 20:37:33 +01:00
liv 149e0c8ac2
Merge pull request #1370 from magnusrodseth/main
docs: add link to docs about `iter_mut` and `map`
2023-02-13 11:01:48 +01:00
magnusrodseth 48ce9d2fd8 docs: add link to docs about `iter_mut` and `map` 2023-02-12 18:26:13 +01:00
5 changed files with 61 additions and 23 deletions

View File

@ -1839,6 +1839,15 @@
"contributions": [
"content"
]
},
{
"login": "black-puppydog",
"name": "Daan Wynen",
"avatar_url": "https://avatars.githubusercontent.com/u/189241?v=4",
"profile": "https://github.com/black-puppydog",
"contributions": [
"content"
]
}
],
"contributorsPerLine": 8,

View File

@ -262,6 +262,7 @@ authors.
</tr>
<tr>
<td align="center" valign="top" width="12.5%"><a href="https://grzegorz-zur.com/"><img src="https://avatars.githubusercontent.com/u/5297583?v=4?s=100" width="100px;" alt="Grzegorz Żur"/><br /><sub><b>Grzegorz Żur</b></sub></a><br /><a href="#content-grzegorz-zur" title="Content">🖋</a></td>
<td align="center" valign="top" width="12.5%"><a href="https://github.com/black-puppydog"><img src="https://avatars.githubusercontent.com/u/189241?v=4?s=100" width="100px;" alt="Daan Wynen"/><br /><sub><b>Daan Wynen</b></sub></a><br /><a href="#content-black-puppydog" title="Content">🖋</a></td>
</tr>
</tbody>
</table>

View File

@ -4,6 +4,9 @@
// 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.
// I AM NOT DONE
@ -20,29 +23,52 @@ fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
input
}
fn main() {
// No clone occurs because `input` doesn't need to be mutated.
let slice = [0, 1, 2];
let mut input = Cow::from(&slice[..]);
match abs_all(&mut input) {
Cow::Borrowed(_) => println!("I borrowed the slice!"),
_ => panic!("expected borrowed value"),
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reference_mutation() -> Result<(), &'static str> {
// Clone occurs because `input` needs to be mutated.
let slice = [-1, 0, 1];
let mut input = Cow::from(&slice[..]);
match abs_all(&mut input) {
Cow::Owned(_) => Ok(()),
_ => Err("Expected owned value"),
}
}
// Clone occurs because `input` needs to be mutated.
let slice = [-1, 0, 1];
let mut input = Cow::from(&slice[..]);
match abs_all(&mut input) {
Cow::Owned(_) => println!("I modified the slice and now own it!"),
_ => panic!("expected owned value"),
#[test]
fn reference_no_mutation() -> Result<(), &'static str> {
// No clone occurs because `input` doesn't need to be mutated.
let slice = [0, 1, 2];
let mut input = Cow::from(&slice[..]);
match abs_all(&mut input) {
// TODO
}
}
// No clone occurs because `input` is already owned.
let slice = vec![-1, 0, 1];
let mut input = Cow::from(slice);
match abs_all(&mut input) {
// TODO
Cow::Borrowed(_) => println!("I own this slice!"),
_ => panic!("expected borrowed value"),
#[test]
fn owned_no_mutation() -> Result<(), &'static str> {
// We can also pass `slice` without `&` so Cow owns it directly.
// In this case no mutation occurs and thus also no clone,
// but the result is still owned because it always was.
let slice = vec![0, 1, 2];
let mut input = Cow::from(slice);
match abs_all(&mut input) {
// TODO
}
}
#[test]
fn owned_mutation() -> Result<(), &'static str> {
// Of course this is also the case if a mutation does occur.
// In this case the call to `to_mut()` returns a reference to
// the same data as before.
let slice = vec![-1, 0, 1];
let mut input = Cow::from(slice);
match abs_all(&mut input) {
// TODO
}
}
}

View File

@ -13,3 +13,5 @@ the other useful data structure, hash maps, later.
## Further information
- [Storing Lists of Values with Vectors](https://doc.rust-lang.org/stable/book/ch08-01-vectors.html)
- [`iter_mut`](https://doc.rust-lang.org/std/primitive.slice.html#method.iter_mut)
- [`map`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.map)

View File

@ -1010,11 +1010,11 @@ https://doc.rust-lang.org/stable/book/ch16-00-concurrency.html
[[exercises]]
name = "cow1"
path = "exercises/smart_pointers/cow1.rs"
mode = "compile"
mode = "test"
hint = """
Since the vector is already owned, the `Cow` type doesn't need to clone it.
If Cow already owns the data it doesn't need to clone it when to_mut() is called.
Checkout https://doc.rust-lang.org/std/borrow/enum.Cow.html for documentation
Check out https://doc.rust-lang.org/std/borrow/enum.Cow.html for documentation
on the `Cow` type.
"""