Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
baiyazi233 committed Apr 9, 2024
1 parent d947bcd commit 102bae5
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 20 deletions.
18 changes: 14 additions & 4 deletions exercises/iterators/iterators3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


#[derive(Debug, PartialEq, Eq)]
pub enum DivisionError {
Expand All @@ -26,23 +26,33 @@ pub struct NotDivisibleError {
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
// Otherwise, return a suitable error.
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
todo!();
if b == 0 {
Err(DivisionError::DivideByZero)
}
else if a % b == 0 {
Ok(a/b)
}
else {
Err(DivisionError::NotDivisible(NotDivisibleError{dividend: a, divisor: b}))
}
}

// Complete the function and return a value of the correct type so the test
// passes.
// Desired output: Ok([1, 11, 1426, 3])
fn result_with_list() -> () {
fn result_with_list() -> Result<Vec<i32>, DivisionError> {
let numbers = vec![27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27));
division_results.collect()
}

// Complete the function and return a value of the correct type so the test
// passes.
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
fn list_of_results() -> () {
fn list_of_results() -> Vec<Result<i32, DivisionError>> {
let numbers = vec![27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27));
division_results.collect()
}

#[cfg(test)]
Expand Down
12 changes: 11 additions & 1 deletion exercises/iterators/iterators4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


pub fn factorial(num: u64) -> u64 {
// Complete this function to return the factorial of num
Expand All @@ -15,6 +15,16 @@ pub fn factorial(num: u64) -> u64 {
// For an extra challenge, don't use:
// - recursion
// Execute `rustlings hint iterators4` for hints.
match num {
0 => 1,
_ => {
let mut res = 1;
for i in 1..=num {
res *= i;
}
res
}
}
}

#[cfg(test)]
Expand Down
10 changes: 7 additions & 3 deletions exercises/iterators/iterators5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


use std::collections::HashMap;

Expand All @@ -35,7 +35,7 @@ fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
// map is a hashmap with String keys and Progress values.
// map = { "variables1": Complete, "from_str": None, ... }
todo!();
map.values().filter(|&progress| *progress == value).count()
}

fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
Expand All @@ -54,7 +54,11 @@ fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Pr
// collection is a slice of hashmaps.
// collection = [{ "variables1": Complete, "from_str": None, ... },
// { "variables2": Complete, ... }, ... ]
todo!();
collection
.iter()
.flat_map(|map| map.values())
.filter(|&progress| *progress == value)
.count()
}

#[cfg(test)]
Expand Down
6 changes: 3 additions & 3 deletions exercises/smart_pointers/arc1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@
//
// 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);
Expand Down
8 changes: 4 additions & 4 deletions exercises/smart_pointers/box1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
//
// 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,
}

Expand All @@ -35,11 +35,11 @@ fn main() {
}

pub fn create_empty_list() -> List {
todo!()
List::Nil
}

pub fn create_non_empty_list() -> List {
todo!()
List::Cons(1, Box::new(List::Nil))
}

#[cfg(test)]
Expand Down
8 changes: 7 additions & 1 deletion exercises/smart_pointers/cow1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
// Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE


use std::borrow::Cow;

Expand Down Expand Up @@ -49,6 +49,8 @@ mod tests {
let mut input = Cow::from(&slice[..]);
match abs_all(&mut input) {
// TODO
Cow::Borrowed(_) => Ok(()),
_ => Err("Expected borrowed value"),
}
}

Expand All @@ -61,6 +63,8 @@ mod tests {
let mut input = Cow::from(slice);
match abs_all(&mut input) {
// TODO
Cow::Owned(_) => Ok(()),
_ => Err("Expected owned value"),
}
}

Expand All @@ -73,6 +77,8 @@ mod tests {
let mut input = Cow::from(slice);
match abs_all(&mut input) {
// TODO
Cow::Owned(_) => Ok(()),
_ => Err("Expected owned value"),
}
}
}
11 changes: 7 additions & 4 deletions exercises/smart_pointers/rc1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//
// Execute `rustlings hint rc1` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE


use std::rc::Rc;

Expand Down Expand Up @@ -60,17 +60,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();

Expand All @@ -92,12 +92,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);
Expand Down

0 comments on commit 102bae5

Please sign in to comment.