Skip to content

Commit 1af08f2

Browse files
committed
feat: add Cow
1 parent d0fe46a commit 1af08f2

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

β€Žsrc/SUMMARY.mdβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
- [Associated Type Bounds](rust/r3/associated-type-bounds.md)
4343
- [BoxStream](rust/r3/futures-boxstream.md)
4444
- [IndexMap](rust/r3/indexmap.md)
45+
- [Cow](rust/r3/cow.md)
4546
- [R2 - Expert](rust/r2/mod.md)
4647
- [Hello Github Action](rust/r2/hello-github-action.md)
4748
- [Hello Actix CloudRun](rust/r2/hello-actix-cloudrun.md)

β€Žsrc/rust/r3/cow.mdβ€Ž

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# `Cow` πŸ„ aka `Copy on write`
2+
3+
![](/assets/kat.png) <span class="speech-bubble"> Let's talk about `Cow` πŸ„ aka `Copy on write`</span>
4+
5+
```rust,editable
6+
use std::borrow::Cow;
7+
8+
fn main() {
9+
println!("πŸ›ΈπŸ’¨ Welcome to the Farm (and Alien Abduction) Fun Zone! πŸ„\n");
10+
11+
// --- πŸ„ Our First Cow: Just Grazing Peacefully ---
12+
// This cow is just chillin' in the field, like a borrowed string.
13+
let mut peaceful_cow: Cow<'static, str> = Cow::Borrowed("Mooo, just grazing... 🌿");
14+
println!("1. Peaceful Cow: {}", peaceful_cow);
15+
println!(" (It's borrowed! Just observing this cow. 🧐)\n");
16+
17+
// --- πŸ‘½ UFO Appears! Time to SUCK the Cow! ---
18+
// The UFO wants to "modify" the cow (i.e., abduct it!).
19+
// If it's a borrowed cow, the UFO needs to make its OWN copy to take!
20+
// (This simulates `to_mut()` forcing a copy for modification)
21+
println!("2. OH NO! A UFO appears! πŸ‘½");
22+
peaceful_cow.to_mut().push_str(" AIEEEE! I'm being beamed up! ⬆️");
23+
println!(" Cow after abduction attempt: {}", peaceful_cow);
24+
println!(" (Since it was borrowed, the UFO made a *copy* to abduct. The original is safe... for now! πŸš€)\n");
25+
26+
27+
// --- πŸ„ Our Second Cow: Already in Its Own Barn ---
28+
// This cow is already "owned" by us, like an owned String.
29+
let mut owned_cow: Cow<'_, str> = Cow::Owned(String::from("Mooo, I'm safe in MY barn! 🏑"));
30+
println!("3. Owned Cow: {}", owned_cow);
31+
println!(" (This cow is already owned! No initial copy needed. 😎)\n");
32+
33+
// --- πŸ‘½ UFO Appears AGAIN! ---
34+
// The UFO wants to abduct THIS cow too!
35+
// But since this cow is *already owned*, the UFO can just grab *this specific cow*.
36+
// No new copy is needed because it's already "owned" for modification.
37+
println!("4. Another UFO! This one's persistent! πŸ›Έ");
38+
owned_cow.to_mut().push_str(" Help! They're taking MY barn too! πŸ˜΅β€πŸ’«");
39+
println!(" Owned Cow after abduction: {}", owned_cow);
40+
println!(" (It was already owned, so the UFO just took *that* cow. Efficient abduction! πŸ’―)\n");
41+
42+
println!("πŸ›ΈπŸ„πŸ’¨ CoW: Keeping your original data safe from alien abduction, unless they need to make a copy! ✨");
43+
}
44+
```
45+
46+
> πŸ’‘ You can read more about `Cow` here πŸ‘‰ [6 things you can do with the Cow πŸ„ in Rust πŸ¦€](https://dev.to/kgrech/6-things-you-can-do-with-the-cow-in-rust-4l55)

β€Žsrc/rust/r3/create-lib.mdβ€Ž

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,3 @@ fn hello(foo: &str) -> Cow<'_, str>
7979
// Take impl, return impl.
8080
fn hello(foo: impl AsRef<str>) -> impl AsRef<str>
8181
```
82-
83-
> πŸ’‘ You can read more about `Cow` here πŸ‘‰ [6 things you can do with the Cow πŸ„ in Rust πŸ¦€](https://dev.to/kgrech/6-things-you-can-do-with-the-cow-in-rust-4l55)

0 commit comments

Comments
Β (0)