mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-12 21:59:18 +00:00
day10
This commit is contained in:
parent
d24adeb230
commit
012359ddfc
@ -12,25 +12,22 @@
|
|||||||
// Because we are using threads, our values need to be thread-safe. Therefore,
|
// Because we are using threads, our values need to be thread-safe. Therefore,
|
||||||
// we are using Arc. We need to make a change in each of the two TODOs.
|
// we are using Arc. We need to make a change in each of the two TODOs.
|
||||||
|
|
||||||
|
|
||||||
// Make this code compile by filling in a value for `shared_numbers` where the
|
// Make this code compile by filling in a value for `shared_numbers` where the
|
||||||
// first TODO comment is, and create an initial binding for `child_numbers`
|
// first TODO comment is, and create an initial binding for `child_numbers`
|
||||||
// where the second TODO comment is. Try not to create any copies of the `numbers` Vec!
|
// where the second TODO comment is. Try not to create any copies of the `numbers` Vec!
|
||||||
// Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint.
|
// 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.
|
#![forbid(unused_imports)] // Do not change this, (or the next) line.
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let numbers: Vec<_> = (0..100u32).collect();
|
let numbers: Vec<_> = (0..100u32).collect();
|
||||||
let shared_numbers = // TODO
|
let shared_numbers = Arc::new(numbers); // TODO
|
||||||
let mut joinhandles = Vec::new();
|
let mut joinhandles = Vec::new();
|
||||||
|
|
||||||
for offset in 0..8 {
|
for offset in 0..8 {
|
||||||
let child_numbers = // TODO
|
let child_numbers = shared_numbers.clone(); // TODO
|
||||||
joinhandles.push(thread::spawn(move || {
|
joinhandles.push(thread::spawn(move || {
|
||||||
let sum: u32 = child_numbers.iter().filter(|n| *n % 8 == offset).sum();
|
let sum: u32 = child_numbers.iter().filter(|n| *n % 8 == offset).sum();
|
||||||
println!("Sum of offset {} is {}", offset, sum);
|
println!("Sum of offset {} is {}", offset, sum);
|
||||||
|
|||||||
@ -16,11 +16,9 @@
|
|||||||
//
|
//
|
||||||
// Execute `rustlings hint box1` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint box1` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
pub enum List {
|
pub enum List {
|
||||||
Cons(i32, List),
|
Cons(i32, Box<List>),
|
||||||
Nil,
|
Nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,11 +31,13 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_empty_list() -> List {
|
pub fn create_empty_list() -> List {
|
||||||
todo!()
|
// todo!()
|
||||||
|
List::Nil
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_non_empty_list() -> List {
|
pub fn create_non_empty_list() -> List {
|
||||||
todo!()
|
// todo!()
|
||||||
|
List::Cons(1, Box::new(List::Nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@ -5,8 +5,6 @@
|
|||||||
// It can enclose and provide immutable access to borrowed data, and clone the data lazily when mutation or ownership is required.
|
// It can enclose and provide immutable access to borrowed data, and clone the data lazily when mutation or ownership is required.
|
||||||
// The type is designed to work with general borrowed data via the Borrow trait.
|
// The type is designed to work with general borrowed data via the Borrow trait.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
|
fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
|
||||||
@ -42,7 +40,7 @@ fn main() {
|
|||||||
let mut input = Cow::from(slice);
|
let mut input = Cow::from(slice);
|
||||||
match abs_all(&mut input) {
|
match abs_all(&mut input) {
|
||||||
// TODO
|
// TODO
|
||||||
Cow::Borrowed(_) => println!("I own this slice!"),
|
Cow::Owned(_) => println!("I own this slice!"),
|
||||||
_ => panic!("expected borrowed value"),
|
_ => panic!("expected borrowed value"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,8 +13,6 @@
|
|||||||
//
|
//
|
||||||
// Make the code compile and the tests pass.
|
// Make the code compile and the tests pass.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||||
|
|||||||
@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
// Make this code compile by using the proper Rc primitives to express that the sun has multiple owners.
|
// Make this code compile by using the proper Rc primitives to express that the sun has multiple owners.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -54,17 +53,17 @@ fn main() {
|
|||||||
jupiter.details();
|
jupiter.details();
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
let saturn = Planet::Saturn(Rc::new(Sun {}));
|
let saturn = Planet::Saturn(Rc::clone(&sun));
|
||||||
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
|
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
|
||||||
saturn.details();
|
saturn.details();
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
let uranus = Planet::Uranus(Rc::new(Sun {}));
|
let uranus = Planet::Uranus(Rc::clone(&sun));
|
||||||
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
|
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
|
||||||
uranus.details();
|
uranus.details();
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
let neptune = Planet::Neptune(Rc::new(Sun {}));
|
let neptune = Planet::Neptune(Rc::clone(&sun));
|
||||||
println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
|
println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
|
||||||
neptune.details();
|
neptune.details();
|
||||||
|
|
||||||
@ -86,12 +85,15 @@ fn main() {
|
|||||||
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
|
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
|
drop(earth);
|
||||||
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
|
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
|
drop(venus);
|
||||||
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
|
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
|
drop(mercury);
|
||||||
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
|
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
|
||||||
|
|
||||||
assert_eq!(Rc::strong_count(&sun), 1);
|
assert_eq!(Rc::strong_count(&sun), 1);
|
||||||
|
|||||||
@ -2,30 +2,26 @@
|
|||||||
// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a hint.
|
||||||
// This program should wait until all the spawned threads have finished before exiting.
|
// This program should wait until all the spawned threads have finished before exiting.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
let mut handles = vec![];
|
let mut handles = vec![];
|
||||||
for i in 0..10 {
|
for i in 0..10 {
|
||||||
thread::spawn(move || {
|
handles.push(thread::spawn(move || {
|
||||||
thread::sleep(Duration::from_millis(250));
|
thread::sleep(Duration::from_millis(250));
|
||||||
println!("thread {} is complete", i);
|
println!("hello from thread {}", i);
|
||||||
});
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut completed_threads = 0;
|
let mut completed_threads = 0;
|
||||||
for handle in handles {
|
for handle in handles {
|
||||||
// TODO: a struct is returned from thread::spawn, can you use it?
|
// TODO: a struct is returned from thread::spawn, can you use it?
|
||||||
|
handle.join().unwrap();
|
||||||
completed_threads += 1;
|
completed_threads += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if completed_threads != 10 {
|
if completed_threads != 10 {
|
||||||
panic!("Oh no! All the spawned threads did not finish!");
|
panic!("Oh no! All the spawned threads did not finish!");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::{Arc, Mutex};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
@ -15,13 +15,15 @@ struct JobStatus {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let status = Arc::new(JobStatus { jobs_completed: 0 });
|
let status = Arc::new(JobStatus { jobs_completed: 0 });
|
||||||
|
let mtx = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
|
||||||
let mut handles = vec![];
|
let mut handles = vec![];
|
||||||
for _ in 0..10 {
|
for _ in 0..10 {
|
||||||
let status_shared = Arc::clone(&status);
|
let status_shared = Arc::clone(&status);
|
||||||
|
let mtx = Arc::clone(&mtx);
|
||||||
let handle = thread::spawn(move || {
|
let handle = thread::spawn(move || {
|
||||||
thread::sleep(Duration::from_millis(250));
|
thread::sleep(Duration::from_millis(250));
|
||||||
// TODO: You must take an action before you update a shared value
|
// TODO: You must take an action before you update a shared value
|
||||||
status_shared.jobs_completed += 1;
|
mtx.lock().unwrap().jobs_completed += 1
|
||||||
});
|
});
|
||||||
handles.push(handle);
|
handles.push(handle);
|
||||||
}
|
}
|
||||||
@ -29,6 +31,6 @@ fn main() {
|
|||||||
handle.join().unwrap();
|
handle.join().unwrap();
|
||||||
// TODO: Print the value of the JobStatus.jobs_completed. Did you notice anything
|
// TODO: Print the value of the JobStatus.jobs_completed. Did you notice anything
|
||||||
// interesting in the output? Do you have to 'join' on all the handles?
|
// interesting in the output? Do you have to 'join' on all the handles?
|
||||||
println!("jobs completed {}", ???);
|
println!("jobs completed {:?}", mtx.lock().unwrap().jobs_completed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,7 @@ impl Licensed for SomeSoftware {}
|
|||||||
impl Licensed for OtherSoftware {}
|
impl Licensed for OtherSoftware {}
|
||||||
|
|
||||||
// YOU MAY ONLY CHANGE THE NEXT LINE
|
// YOU MAY ONLY CHANGE THE NEXT LINE
|
||||||
fn compare_license_types(software: &impl Licensed, software_two: &impl Licensed) -> bool {
|
fn compare_license_types(software: impl Licensed, software_two: impl Licensed) -> bool {
|
||||||
software.licensing_info() == software_two.licensing_info()
|
software.licensing_info() == software_two.licensing_info()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user