Skip to content

Commit 5cd1927

Browse files
committed
update original
1 parent 3ee0cf4 commit 5cd1927

4 files changed

Lines changed: 71 additions & 8 deletions

File tree

rust-by-example/src/error/option_unwrap/question_mark.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct Job {
2828
}
2929
3030
#[derive(Clone, Copy)]
31+
#[allow(dead_code)]
3132
struct PhoneNumber {
3233
area_code: Option<u8>,
3334
number: u32,

rust-by-example/src/scope/lifetime/static_lifetime.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ live for the entire duration, but only from the leaking point onward.
7373
extern crate rand;
7474
use rand::Fill;
7575
76-
fn random_vec() -> &'static [usize; 100] {
77-
let mut rng = rand::thread_rng();
76+
fn random_vec() -> &'static [u64; 100] {
77+
let mut rng = rand::rng();
7878
let mut boxed = Box::new([0; 100]);
79-
boxed.try_fill(&mut rng).unwrap();
79+
boxed.fill(&mut rng);
8080
Box::leak(boxed)
8181
}
8282
8383
fn main() {
84-
let first: &'static [usize; 100] = random_vec();
85-
let second: &'static [usize; 100] = random_vec();
84+
let first: &'static [u64; 100] = random_vec();
85+
let second: &'static [u64; 100] = random_vec();
8686
assert_ne!(first, second)
8787
}
8888
```

rust-by-example/src/trait/clone.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Clone
1+
# Clone and Copy
22

33
When dealing with resources, the default behavior is to transfer them during
44
assignments or function calls. However, sometimes we need to make a
@@ -7,19 +7,37 @@ copy of the resource as well.
77
The [`Clone`][clone] trait helps us do exactly this. Most commonly, we can
88
use the `.clone()` method defined by the `Clone` trait.
99

10+
## Copy: Implicit Cloning
11+
12+
The [`Copy`][copy] trait allows a type to be duplicated simply by copying bits,
13+
with no additional logic required. When a type implements `Copy`, assignments
14+
and function calls will implicitly copy the value instead of moving it.
15+
16+
**Important:** `Copy` requires `Clone` - any type that implements `Copy` must
17+
also implement `Clone`. This is because `Copy` is defined as a subtrait:
18+
`trait Copy: Clone {}`. The `Clone` implementation for `Copy` types simply
19+
copies the bits.
20+
21+
Not all types can implement `Copy`. A type can only be `Copy` if:
22+
- All of its components are `Copy`
23+
- It doesn't manage external resources (like heap memory, file handles, etc.)
24+
1025
```rust,editable
1126
// A unit struct without resources
27+
// Note: Copy requires Clone, so we must derive both
1228
#[derive(Debug, Clone, Copy)]
1329
struct Unit;
1430
1531
// A tuple struct with resources that implements the `Clone` trait
32+
// This CANNOT be Copy because Box<T> is not Copy
1633
#[derive(Clone, Debug)]
1734
struct Pair(Box<i32>, Box<i32>);
1835
1936
fn main() {
2037
// Instantiate `Unit`
2138
let unit = Unit;
22-
// Copy `Unit`, there are no resources to move
39+
// Copy `Unit` - this is an implicit copy, not a move!
40+
// Because Unit implements Copy, the value is duplicated automatically
2341
let copied_unit = unit;
2442
2543
// Both `Unit`s can be used independently
@@ -31,6 +49,7 @@ fn main() {
3149
println!("original: {:?}", pair);
3250
3351
// Move `pair` into `moved_pair`, moves resources
52+
// Pair does not implement Copy, so this is a move
3453
let moved_pair = pair;
3554
println!("moved: {:?}", moved_pair);
3655
@@ -39,6 +58,7 @@ fn main() {
3958
// TODO ^ Try uncommenting this line
4059
4160
// Clone `moved_pair` into `cloned_pair` (resources are included)
61+
// Unlike Copy, Clone is explicit - we must call .clone()
4262
let cloned_pair = moved_pair.clone();
4363
// Drop the moved original pair using std::mem::drop
4464
drop(moved_pair);
@@ -53,3 +73,4 @@ fn main() {
5373
```
5474

5575
[clone]: https://doc.rust-lang.org/std/clone/trait.Clone.html
76+
[copy]: https://doc.rust-lang.org/std/marker/trait.Copy.html

rust-by-example/src/trait/supertraits.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,48 @@ fn comp_sci_student_greeting(student: &dyn CompSciStudent) -> String {
3434
)
3535
}
3636
37-
fn main() {}
37+
struct CSStudent {
38+
name: String,
39+
university: String,
40+
fav_language: String,
41+
git_username: String
42+
}
43+
44+
impl Programmer for CSStudent {
45+
fn fav_language(&self) -> String {
46+
self.fav_language.clone()
47+
}
48+
}
49+
50+
impl Student for CSStudent {
51+
fn university(&self) -> String {
52+
self.university.clone()
53+
}
54+
}
55+
56+
impl Person for CSStudent {
57+
fn name(&self) -> String {
58+
self.name.clone()
59+
}
60+
}
61+
62+
impl CompSciStudent for CSStudent {
63+
fn git_username(&self) -> String {
64+
self.git_username.clone()
65+
}
66+
}
67+
68+
fn main() {
69+
let student = CSStudent {
70+
name: String::from("Alice"),
71+
university: String::from("MIT"),
72+
fav_language: String::from("Rust"),
73+
git_username: String::from("alice_codes"),
74+
};
75+
76+
let greeting = comp_sci_student_greeting(&student);
77+
println!("{}", greeting);
78+
}
3879
```
3980

4081
### See also:

0 commit comments

Comments
 (0)