mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 21:29:18 +00:00
done till exercises/smart_pointers/box1
This commit is contained in:
parent
b84ba2b903
commit
f15058ab29
58
Cargo.lock
generated
58
Cargo.lock
generated
@ -171,6 +171,17 @@ version = "0.3.3"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
|
checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getrandom"
|
||||||
|
version = "0.2.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if 1.0.0",
|
||||||
|
"libc",
|
||||||
|
"wasi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "glob"
|
name = "glob"
|
||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
@ -266,9 +277,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.100"
|
version = "0.2.139"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a1fa8cddc8fbbee11227ef194b5317ed014b8acbf15139bd716a18ad3fe99ec5"
|
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "log"
|
name = "log"
|
||||||
@ -384,6 +395,12 @@ version = "1.10.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
|
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ppv-lite86"
|
||||||
|
version = "0.2.17"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "predicates"
|
name = "predicates"
|
||||||
version = "1.0.8"
|
version = "1.0.8"
|
||||||
@ -431,6 +448,36 @@ dependencies = [
|
|||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand"
|
||||||
|
version = "0.8.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"rand_chacha",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_chacha"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||||
|
dependencies = [
|
||||||
|
"ppv-lite86",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.6.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.2.10"
|
version = "0.2.10"
|
||||||
@ -469,6 +516,7 @@ dependencies = [
|
|||||||
"indicatif",
|
"indicatif",
|
||||||
"notify",
|
"notify",
|
||||||
"predicates",
|
"predicates",
|
||||||
|
"rand",
|
||||||
"regex",
|
"regex",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
@ -592,6 +640,12 @@ dependencies = [
|
|||||||
"winapi-util",
|
"winapi-util",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasi"
|
||||||
|
version = "0.11.0+wasi-snapshot-preview1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi"
|
name = "winapi"
|
||||||
version = "0.2.8"
|
version = "0.2.8"
|
||||||
|
|||||||
@ -18,6 +18,7 @@ serde = { version = "1.0", features = ["derive"] }
|
|||||||
serde_json = "1.0.81"
|
serde_json = "1.0.81"
|
||||||
home = "0.5.3"
|
home = "0.5.3"
|
||||||
glob = "0.3.0"
|
glob = "0.3.0"
|
||||||
|
rand = "0.8.5"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "rustlings"
|
name = "rustlings"
|
||||||
@ -27,3 +28,4 @@ path = "src/main.rs"
|
|||||||
assert_cmd = "0.11.0"
|
assert_cmd = "0.11.0"
|
||||||
predicates = "1.0.1"
|
predicates = "1.0.1"
|
||||||
glob = "0.3.0"
|
glob = "0.3.0"
|
||||||
|
rand = "0.8.5"
|
||||||
|
|||||||
@ -6,13 +6,14 @@
|
|||||||
// The program should wait until all the spawned threads have finished and
|
// The program should wait until all the spawned threads have finished and
|
||||||
// should collect their return values into a vector.
|
// should collect their return values into a vector.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
|
// use rand::Rng;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut handles = vec![];
|
let mut handles = vec![];
|
||||||
|
// let rng = rand::thread_rng();
|
||||||
for i in 0..10 {
|
for i in 0..10 {
|
||||||
handles.push(thread::spawn(move || {
|
handles.push(thread::spawn(move || {
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
@ -22,9 +23,11 @@ fn main() {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
print!("next move, handler len {} \n", handles.len());
|
||||||
let mut results: Vec<u128> = vec![];
|
let mut results: Vec<u128> = vec![];
|
||||||
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?
|
||||||
|
results.push(handle.join().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
if results.len() != 10 {
|
if results.len() != 10 {
|
||||||
|
|||||||
@ -3,9 +3,7 @@
|
|||||||
// Building on the last exercise, we want all of the threads to complete their work but this time
|
// Building on the last exercise, we want all of the threads to complete their work but this time
|
||||||
// the spawned threads need to be in charge of updating a shared value: JobStatus.jobs_completed
|
// the spawned threads need to be in charge of updating a shared value: JobStatus.jobs_completed
|
||||||
|
|
||||||
// I AM NOT DONE
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
@ -14,14 +12,14 @@ struct JobStatus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let status = Arc::new(JobStatus { jobs_completed: 0 });
|
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
|
||||||
let mut handles = vec![];
|
let mut handles = vec![];
|
||||||
for _ in 0..10 {
|
for i in 0..10 {
|
||||||
let status_shared = Arc::clone(&status);
|
let status_shared = Arc::clone(&status);
|
||||||
let handle = thread::spawn(move || {
|
let handle = thread::spawn(move || {
|
||||||
thread::sleep(Duration::from_millis(250));
|
thread::sleep(Duration::from_millis(250 + i * 10));
|
||||||
// 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;
|
status_shared.lock().unwrap().jobs_completed += 1;
|
||||||
});
|
});
|
||||||
handles.push(handle);
|
handles.push(handle);
|
||||||
}
|
}
|
||||||
@ -29,6 +27,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 {}", status.lock().unwrap().jobs_completed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,6 @@
|
|||||||
// threads3.rs
|
// threads3.rs
|
||||||
// Execute `rustlings hint threads3` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint threads3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
@ -28,6 +26,7 @@ fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
|
|||||||
let qc = Arc::new(q);
|
let qc = Arc::new(q);
|
||||||
let qc1 = Arc::clone(&qc);
|
let qc1 = Arc::clone(&qc);
|
||||||
let qc2 = Arc::clone(&qc);
|
let qc2 = Arc::clone(&qc);
|
||||||
|
let tx1 = tx.clone();
|
||||||
|
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
for val in &qc1.first_half {
|
for val in &qc1.first_half {
|
||||||
@ -40,7 +39,7 @@ fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
|
|||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
for val in &qc2.second_half {
|
for val in &qc2.second_half {
|
||||||
println!("sending {:?}", val);
|
println!("sending {:?}", val);
|
||||||
tx.send(*val).unwrap();
|
tx1.send(*val).unwrap();
|
||||||
thread::sleep(Duration::from_secs(1));
|
thread::sleep(Duration::from_secs(1));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user