Skip to content

Commit 9b48790

Browse files
committed
Question improvements
1 parent 48a2f91 commit 9b48790

File tree

8 files changed

+87
-216
lines changed

8 files changed

+87
-216
lines changed

questions/q027.yaml

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
question: |-
22
```rust
3-
use std::rc::Rc;
4-
use std::sync::Arc;
5-
use std::thread;
3+
use std::rc::Rc;
4+
use std::sync::Arc;
5+
use std::thread;
66
7-
// Version A
8-
fn version_a() {
9-
let data = Rc::new(vec![1, 2, 3]);
10-
let data_clone = Rc::clone(&data);
11-
12-
thread::spawn(move || {
13-
println!("{:?}", data_clone);
14-
});
15-
}
7+
// Version A
8+
fn version_a() {
9+
let data = Rc::new(vec![1, 2, 3]);
10+
let data_clone = Rc::clone(&data);
11+
12+
thread::spawn(move || {
13+
println!("{:?}", data_clone);
14+
}).join();
15+
}
1616
17-
// Version B
18-
fn version_b() {
19-
let data = Arc::new(vec![1, 2, 3]);
20-
let data_clone = Arc::clone(&data);
21-
22-
thread::spawn(move || {
23-
println!("{:?}", data_clone);
24-
});
25-
}
17+
// Version B
18+
fn version_b() {
19+
let data = Arc::new(vec![1, 2, 3]);
20+
let data_clone = Arc::clone(&data);
21+
22+
thread::spawn(move || {
23+
println!("{:?}", data_clone);
24+
}).join();
25+
}
2626
27-
fn main() {
28-
// Uncommenting version_a() would cause a compilation error
29-
version_a();
30-
// version_b();
31-
}
27+
fn main() {
28+
version_a();
29+
version_b();
30+
}
3231
```
3332
3433
Which code compiles?

questions/q038.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
question: |-
22
```rust
3+
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
4+
if x.len() > y.len() {
5+
x
6+
} else {
7+
y
8+
}
9+
}
10+
311
fn main() {
412
let string1 = String::from("long string");
513
let result;
@@ -9,14 +17,6 @@ question: |-
917
}
1018
println!("{}", result);
1119
}
12-
13-
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
14-
if x.len() > y.len() {
15-
x
16-
} else {
17-
y
18-
}
19-
}
2020
```
2121
2222
What happens when this code runs?

questions/q064.yaml

Lines changed: 0 additions & 52 deletions
This file was deleted.

questions/q073.yaml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,15 @@ explanation: |-
6464
6565
```rust
6666
trait Plugin: Any {
67-
fn name(&self) -> &str;
68-
fn execute(&self);
69-
70-
fn as_any(&self) -> &dyn Any {
71-
self
72-
}
67+
fn as_any(&self) -> &dyn Any;
68+
}
69+
70+
impl Plugin for AudioPlugin {
71+
fn as_any(&self) -> &dyn Any { self }
72+
}
73+
74+
impl Plugin for VideoPlugin {
75+
fn as_any(&self) -> &dyn Any { self }
7376
}
7477
```
7578

questions/q078.yaml

Lines changed: 0 additions & 60 deletions
This file was deleted.

questions/q081.yaml

Lines changed: 0 additions & 63 deletions
This file was deleted.

questions/q210.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ question: |-
2121
What happens when you try to compile this code?
2222
answers:
2323
- The code compiles and runs successfully
24-
- 'Compilation error: the trait `MyTrait` is not dyn compatible because method `method` references the `Self` type in its return type'
24+
- 'Compilation error: the trait `MyTrait` is not dyn compatible
2525
- 'Compilation error: `MyStruct` does not implement `MyTrait`'
2626
- Runtime panic occurs when creating the trait object
2727
correct_answer: 1

questions/q342.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
question: |-
2+
```rust
3+
fn main() {
4+
let mut s = String::from("hello");
5+
let r1 = &s;
6+
let r2 = &s;
7+
drop(r1);
8+
drop(r2);
9+
println!("{} {}", r1, r2);
10+
}
11+
```
12+
13+
What happens when you run this code?
14+
answers:
15+
- Prints "hello hello"
16+
- 'Compilation error: borrow of moved value'
17+
- 'Runtime panic: use after drop'
18+
- 'Compilation error: cannot drop a reference'
19+
correct_answer: 0
20+
expected_output:
21+
- 'hello hello'
22+
explanation: |-
23+
This question tests understanding of reference semantics and the `drop()` function
24+
in Rust. The key misconception is thinking that calling `drop()` on a reference
25+
will invalidate it or move it, making it unusable afterward.
26+
27+
In reality, references are `Copy` types, meaning they are copied (not moved) when
28+
passed to functions. When `drop(r1)` is called, a copy of the reference is passed
29+
to the `drop()` function, and that copy is dropped. The original binding `r1`
30+
remains valid and unchanged. The same applies to `r2`.
31+
32+
After both `drop()` calls, `r1` and `r2` are still valid references pointing to
33+
the string `s`, so `println!("{} {}", r1, r2)` successfully prints `hello hello`.
34+
35+
Calling `drop()` on a reference is effectively a no-op—it drops a copy of the
36+
reference, not the referenced data itself. This is fundamentally different from
37+
calling `drop()` on an owned value, which would move the value and make it
38+
inaccessible.
39+
40+
The key takeaway is that references implement `Copy`, so passing them to functions
41+
(including `drop()`) creates copies rather than moves. Dropping a reference has no
42+
effect on either the reference binding or the underlying data. To actually
43+
invalidate references, you would need to drop or mutate the owned value they refer
44+
to, not the references themselves.

0 commit comments

Comments
 (0)