Ejercicios 82/94
This commit is contained in:
parent
8716d57ae4
commit
39ac0b9f12
|
@ -18,19 +18,17 @@
|
||||||
// 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);
|
||||||
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);
|
||||||
|
|
|
@ -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,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(1, Box::new(List::Cons(2, 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]> {
|
||||||
|
@ -41,8 +39,7 @@ fn main() {
|
||||||
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(_) => println!("I own this slice!"),
|
||||||
Cow::Borrowed(_) => println!("I own this slice!"),
|
|
||||||
_ => panic!("expected borrowed value"),
|
_ => panic!("expected borrowed value"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +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,18 +52,15 @@ fn main() {
|
||||||
println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
|
println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
|
||||||
jupiter.details();
|
jupiter.details();
|
||||||
|
|
||||||
// TODO
|
let saturn = Planet::Saturn(Rc::clone(&sun));
|
||||||
let saturn = Planet::Saturn(Rc::new(Sun {}));
|
|
||||||
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
|
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
|
||||||
saturn.details();
|
saturn.details();
|
||||||
|
|
||||||
// TODO
|
let uranus = Planet::Uranus(Rc::clone(&sun));
|
||||||
let uranus = Planet::Uranus(Rc::new(Sun {}));
|
|
||||||
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
|
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
|
||||||
uranus.details();
|
uranus.details();
|
||||||
|
|
||||||
// TODO
|
let neptune = Planet::Neptune(Rc::clone(&sun));
|
||||||
let neptune = Planet::Neptune(Rc::new(Sun {}));
|
|
||||||
println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
|
println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
|
||||||
neptune.details();
|
neptune.details();
|
||||||
|
|
||||||
|
@ -86,13 +81,13 @@ fn main() {
|
||||||
drop(mars);
|
drop(mars);
|
||||||
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
|
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
|
||||||
|
|
||||||
// TODO
|
drop(earth);
|
||||||
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
|
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
|
||||||
|
|
||||||
// TODO
|
drop(venus);
|
||||||
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
|
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
Loading…
Reference in New Issue