This commit is contained in:
Tirth Patel 2024-05-23 22:31:36 -02:30
parent 71a17b5616
commit b2fdc401d5
4 changed files with 18 additions and 17 deletions

View File

@ -21,7 +21,6 @@
// //
// 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;
@ -29,11 +28,11 @@ 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);
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 = Arc::clone(&shared_numbers);
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);

View File

@ -18,11 +18,10 @@
// //
// 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,
} }
@ -35,11 +34,11 @@ fn main() {
} }
pub fn create_empty_list() -> List { pub fn create_empty_list() -> List {
todo!() List::Nil
} }
pub fn create_non_empty_list() -> List { pub fn create_non_empty_list() -> List {
todo!() List::Cons(10, Box::new(List::Nil))
} }
#[cfg(test)] #[cfg(test)]

View File

@ -12,7 +12,6 @@
// //
// Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
use std::borrow::Cow; use std::borrow::Cow;
@ -37,7 +36,7 @@ mod tests {
let slice = [-1, 0, 1]; let slice = [-1, 0, 1];
let mut input = Cow::from(&slice[..]); let mut input = Cow::from(&slice[..]);
match abs_all(&mut input) { match abs_all(&mut input) {
Cow::Owned(_) => Ok(()), Cow::Owned(vec) => Ok(assert_eq!(*vec, vec![1,0,1])),
_ => Err("Expected owned value"), _ => Err("Expected owned value"),
} }
} }
@ -48,7 +47,8 @@ mod tests {
let slice = [0, 1, 2]; let slice = [0, 1, 2];
let mut input = Cow::from(&slice[..]); let mut input = Cow::from(&slice[..]);
match abs_all(&mut input) { match abs_all(&mut input) {
// TODO Cow::Borrowed(vec) => Ok(assert_eq!(*vec, vec![0,1,2])),
_ => Err("Expected owned value"),
} }
} }
@ -60,7 +60,8 @@ mod tests {
let slice = vec![0, 1, 2]; let slice = vec![0, 1, 2];
let mut input = Cow::from(slice); let mut input = Cow::from(slice);
match abs_all(&mut input) { match abs_all(&mut input) {
// TODO Cow::Owned(vec) => Ok(assert_eq!(*vec, vec![0,1,2])),
_ => Err("Expected owned value"),
} }
} }
@ -72,7 +73,8 @@ mod tests {
let slice = vec![-1, 0, 1]; let slice = vec![-1, 0, 1];
let mut input = Cow::from(slice); let mut input = Cow::from(slice);
match abs_all(&mut input) { match abs_all(&mut input) {
// TODO Cow::Owned(vec) => Ok(assert_eq!(*vec, vec![1,0,1])),
_ => Err("Expected owned value"),
} }
} }
} }

View File

@ -10,8 +10,6 @@
// //
// Execute `rustlings hint rc1` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint rc1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
use std::rc::Rc; use std::rc::Rc;
#[derive(Debug)] #[derive(Debug)]
@ -61,17 +59,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();
@ -93,12 +91,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);