Compare commits

...

5 Commits

Author SHA1 Message Date
Dylan Smith
7c3575e6ac Complete thread1 and thread2 2024-04-19 16:18:07 -04:00
Dylan Smith
f222629213 Finish thread1 2024-04-19 15:55:02 -04:00
Dylan Smith
5484d34a98 arc and moo cow 2024-04-19 15:13:03 -04:00
Dylan Smith
ad26c3a4d2 Finish reference count smart pointer 2024-04-19 14:31:51 -04:00
Dylan Smith
44e3362386 Finish Box 2024-04-19 13:56:27 -04:00
6 changed files with 31 additions and 30 deletions

View File

@@ -21,19 +21,17 @@
//
// Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
#![forbid(unused_imports)] // Do not change this, (or the next) line.
use std::sync::Arc;
use std::thread;
fn main() {
let numbers: Vec<_> = (0..100u32).collect();
let shared_numbers = // TODO
let shared_numbers = Arc::new(numbers); // TODO
let mut joinhandles = Vec::new();
for offset in 0..8 {
let child_numbers = // TODO
let child_numbers = Arc::clone(&shared_numbers); // TODO
joinhandles.push(thread::spawn(move || {
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
println!("Sum of offset {} is {}", offset, sum);

View File

@@ -18,11 +18,9 @@
//
// Execute `rustlings hint box1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
#[derive(PartialEq, Debug)]
pub enum List {
Cons(i32, List),
Cons(i32, Box<List>),
Nil,
}
@@ -35,11 +33,11 @@ fn main() {
}
pub fn create_empty_list() -> List {
todo!()
List::Nil
}
pub fn create_non_empty_list() -> List {
todo!()
List::Cons(3, Box::new(List::Nil))
}
#[cfg(test)]

View File

@@ -12,8 +12,6 @@
//
// Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
use std::borrow::Cow;
fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
@@ -49,6 +47,8 @@ mod tests {
let mut input = Cow::from(&slice[..]);
match abs_all(&mut input) {
// TODO
Cow::Borrowed(_) => Ok(()),
_ => Err("Expected a reference I suppose?"),
}
}
@@ -61,6 +61,8 @@ mod tests {
let mut input = Cow::from(slice);
match abs_all(&mut input) {
// TODO
Cow::Owned(_) => Ok(()),
_ => Err("Expected owned value"),
}
}
@@ -73,6 +75,8 @@ mod tests {
let mut input = Cow::from(slice);
match abs_all(&mut input) {
// TODO
Cow::Owned(_) => Ok(()),
_ => Err("Expected owned value"),
}
}
}

View File

@@ -10,8 +10,6 @@
//
// Execute `rustlings hint rc1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
use std::rc::Rc;
#[derive(Debug)]
@@ -61,17 +59,17 @@ fn main() {
jupiter.details();
// TODO
let saturn = Planet::Saturn(Rc::new(Sun {}));
let saturn = Planet::Saturn(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
saturn.details();
// TODO
let uranus = Planet::Uranus(Rc::new(Sun {}));
let uranus = Planet::Uranus(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
uranus.details();
// TODO
let neptune = Planet::Neptune(Rc::new(Sun {}));
let neptune = Planet::Neptune(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
neptune.details();
@@ -93,12 +91,15 @@ fn main() {
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
// TODO
drop(earth);
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
// TODO
drop(venus);
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
// TODO
drop(mercury);
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
assert_eq!(Rc::strong_count(&sun), 1);

View File

@@ -8,24 +8,25 @@
// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a
// hint.
// I AM NOT DONE
use std::thread;
use std::time::{Duration, Instant};
fn thread_func(i: u32) -> u128 {
let start = Instant::now();
thread::sleep(Duration::from_millis(250));
println!("thread {} is complete", i);
start.elapsed().as_millis()
}
fn main() {
let mut handles = vec![];
for i in 0..10 {
handles.push(thread::spawn(move || {
let start = Instant::now();
thread::sleep(Duration::from_millis(250));
println!("thread {} is complete", i);
start.elapsed().as_millis()
}));
handles.push(thread::spawn(move || thread_func(i)));
}
let mut results: Vec<u128> = vec![];
for handle in handles {
results.push(handle.join().unwrap());
// TODO: a struct is returned from thread::spawn, can you use it?
}

View File

@@ -7,9 +7,7 @@
// Execute `rustlings hint threads2` or use the `hint` watch subcommand for a
// hint.
// I AM NOT DONE
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
@@ -19,7 +17,7 @@ struct JobStatus {
fn main() {
// TODO: `Arc` isn't enough if you want a **mutable** shared state
let status = Arc::new(JobStatus { jobs_completed: 0 });
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
let mut handles = vec![];
for _ in 0..10 {
@@ -27,7 +25,8 @@ fn main() {
let handle = thread::spawn(move || {
thread::sleep(Duration::from_millis(250));
// TODO: You must take an action before you update a shared value
status_shared.jobs_completed += 1;
let mut s = status_shared.lock().unwrap();
s.jobs_completed += 1;
});
handles.push(handle);
}
@@ -38,5 +37,5 @@ fn main() {
}
// TODO: Print the value of `JobStatus.jobs_completed`
println!("Jobs completed: {}", ???);
println!("Jobs completed: {}", status.lock().unwrap().jobs_completed);
}