Skip to content

Commit bf50f9d

Browse files
committed
update 4
1 parent 31060b9 commit bf50f9d

File tree

7 files changed

+35
-31
lines changed

7 files changed

+35
-31
lines changed

exercises/structs/structs1.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a
66
// hint.
77

8-
// I AM NOT DONE
9-
108
struct ColorClassicStruct {
119
// TODO: Something goes here
10+
red: u8,
11+
green: u8,
12+
blue: u8,
1213
}
1314

14-
struct ColorTupleStruct(/* TODO: Something goes here */);
15+
struct ColorTupleStruct(/* TODO: Something goes here */ u8, u8, u8);
1516

1617
#[derive(Debug)]
1718
struct UnitLikeStruct;
@@ -23,7 +24,11 @@ mod tests {
2324
#[test]
2425
fn classic_c_structs() {
2526
// TODO: Instantiate a classic c struct!
26-
// let green =
27+
let green = ColorClassicStruct {
28+
red: 0,
29+
green: 255,
30+
blue: 0,
31+
};
2732

2833
assert_eq!(green.red, 0);
2934
assert_eq!(green.green, 255);
@@ -33,7 +38,7 @@ mod tests {
3338
#[test]
3439
fn tuple_structs() {
3540
// TODO: Instantiate a tuple struct!
36-
// let green =
41+
let green = ColorTupleStruct(0, 255, 0);
3742

3843
assert_eq!(green.0, 0);
3944
assert_eq!(green.1, 255);
@@ -43,7 +48,7 @@ mod tests {
4348
#[test]
4449
fn unit_structs() {
4550
// TODO: Instantiate a unit-like struct!
46-
// let unit_like_struct =
51+
let unit_like_struct = UnitLikeStruct;
4752
let message = format!("{:?}s are fun!", unit_like_struct);
4853

4954
assert_eq!(message, "UnitLikeStructs are fun!");

exercises/structs/structs2.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// Execute `rustlings hint structs2` or use the `hint` watch subcommand for a
66
// hint.
77

8-
// I AM NOT DONE
9-
108
#[derive(Debug)]
119
struct Order {
1210
name: String,
@@ -38,7 +36,12 @@ mod tests {
3836
fn your_order() {
3937
let order_template = create_order_template();
4038
// TODO: Create your own order using the update syntax and template above!
41-
// let your_order =
39+
let your_order = Order {
40+
name: String::from("Hacker in Rust"),
41+
count: 1,
42+
..order_template
43+
};
44+
4245
assert_eq!(your_order.name, "Hacker in Rust");
4346
assert_eq!(your_order.year, order_template.year);
4447
assert_eq!(your_order.made_by_phone, order_template.made_by_phone);

exercises/structs/structs3.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
// Execute `rustlings hint structs3` or use the `hint` watch subcommand for a
88
// hint.
99

10-
// I AM NOT DONE
11-
1210
#[derive(Debug)]
1311
struct Package {
1412
sender_country: String,
@@ -29,12 +27,12 @@ impl Package {
2927
}
3028
}
3129

32-
fn is_international(&self) -> ??? {
33-
// Something goes here...
30+
fn is_international(&self) -> bool {
31+
self.sender_country != self.recipient_country
3432
}
3533

36-
fn get_fees(&self, cents_per_gram: i32) -> ??? {
37-
// Something goes here...
34+
fn get_fees(&self, cents_per_gram: i32) -> i32 {
35+
self.weight_in_grams * cents_per_gram
3836
}
3937
}
4038

exercises/tests/build.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@ fn main() {
1010
.duration_since(std::time::UNIX_EPOCH)
1111
.unwrap()
1212
.as_secs(); // What's the use of this timestamp here?
13-
let your_command = format!(
14-
"Your command here with {}, please checkout exercises/tests/build.rs",
15-
timestamp
16-
);
13+
let your_command = format!("rustc-env=TEST_FOO={}", timestamp);
1714
println!("cargo:{}", your_command);
1815

1916
// In tests8, we should enable "pass" feature to make the
2017
// testcase return early. Fill in the command to tell
2118
// Cargo about that.
22-
let your_command = "Your command here, please checkout exercises/tests/build.rs";
19+
let your_command = r#"rustc-cfg=feature="pass""#;
2320
println!("cargo:{}", your_command);
2421
}

exercises/threads/threads1.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a
99
// hint.
1010

11-
// I AM NOT DONE
12-
1311
use std::thread;
1412
use std::time::{Duration, Instant};
1513

@@ -27,6 +25,7 @@ fn main() {
2725
let mut results: Vec<u128> = vec![];
2826
for handle in handles {
2927
// TODO: a struct is returned from thread::spawn, can you use it?
28+
results.push(handle.join().unwrap());
3029
}
3130

3231
if results.len() != 10 {

exercises/threads/threads2.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
// Execute `rustlings hint threads2` or use the `hint` watch subcommand for a
88
// hint.
99

10-
// I AM NOT DONE
11-
12-
use std::sync::Arc;
10+
use std::sync::{Arc, Mutex};
1311
use std::thread;
1412
use std::time::Duration;
1513

@@ -18,22 +16,26 @@ struct JobStatus {
1816
}
1917

2018
fn main() {
21-
let status = Arc::new(JobStatus { jobs_completed: 0 });
19+
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
2220
let mut handles = vec![];
21+
2322
for _ in 0..10 {
2423
let status_shared = Arc::clone(&status);
2524
let handle = thread::spawn(move || {
2625
thread::sleep(Duration::from_millis(250));
2726
// TODO: You must take an action before you update a shared value
28-
status_shared.jobs_completed += 1;
27+
let mut status = status_shared.lock().unwrap();
28+
status.jobs_completed += 1;
2929
});
3030
handles.push(handle);
3131
}
32+
3233
for handle in handles {
3334
handle.join().unwrap();
3435
// TODO: Print the value of the JobStatus.jobs_completed. Did you notice
3536
// anything interesting in the output? Do you have to 'join' on all the
3637
// handles?
37-
println!("jobs completed {}", ???);
38+
let status = status.lock().unwrap();
39+
println!("jobs completed {}", status.jobs_completed);
3840
}
3941
}

exercises/threads/threads3.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// Execute `rustlings hint threads3` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
7-
86
use std::sync::mpsc;
97
use std::sync::Arc;
108
use std::thread;
@@ -31,6 +29,8 @@ fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
3129
let qc1 = Arc::clone(&qc);
3230
let qc2 = Arc::clone(&qc);
3331

32+
let tx1 = tx.clone();
33+
3434
thread::spawn(move || {
3535
for val in &qc1.first_half {
3636
println!("sending {:?}", val);
@@ -42,7 +42,7 @@ fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
4242
thread::spawn(move || {
4343
for val in &qc2.second_half {
4444
println!("sending {:?}", val);
45-
tx.send(*val).unwrap();
45+
tx1.send(*val).unwrap();
4646
thread::sleep(Duration::from_secs(1));
4747
}
4848
});

0 commit comments

Comments
 (0)