Standard library types exercises complete

This commit is contained in:
Emre AYDIN 2021-01-07 15:40:40 +03:00
parent 775744ed89
commit 0c9f55747b
6 changed files with 51 additions and 33 deletions

View file

@ -4,18 +4,17 @@
// somewhere. Try not to create any copies of the `numbers` Vec! // somewhere. Try not to create any copies of the `numbers` Vec!
// Execute `rustlings hint arc1` for hints :) // Execute `rustlings hint arc1` for hints :)
// 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 = Arc::clone(&shared_numbers);
joinhandles.push(thread::spawn(move || { joinhandles.push(thread::spawn(move || {
let mut i = offset; let mut i = offset;
let mut sum = 0; let mut sum = 0;

View file

@ -16,11 +16,9 @@
// //
// Execute `rustlings hint box1` for hints :) // Execute `rustlings hint box1` for hints :)
// 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 {
unimplemented!() List::Nil
} }
pub fn create_non_empty_list() -> List { pub fn create_non_empty_list() -> List {
unimplemented!() List::Cons(2, Box::new(create_empty_list()))
} }
#[cfg(test)] #[cfg(test)]

View file

@ -8,17 +8,15 @@
// //
// Execute `rustlings hint iterators1` for hints :D // Execute `rustlings hint iterators1` for hints :D
// I AM NOT DONE
fn main() { fn main() {
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"]; let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
let mut my_iterable_fav_fruits = ???; // TODO: Step 1 let mut my_iterable_fav_fruits = my_fav_fruits.iter(); // TODO: Step 1
assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana")); assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2 assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple")); // TODO: Step 2
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado")); assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2.1 assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach")); // TODO: Step 2.1
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry")); assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3 assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 3
} }

View file

@ -7,16 +7,24 @@
// Try to ensure it returns a single string. // Try to ensure it returns a single string.
// As always, there are hints if you execute `rustlings hint iterators2`! // As always, there are hints if you execute `rustlings hint iterators2`!
// I AM NOT DONE
pub fn capitalize_first(input: &str) -> String { pub fn capitalize_first(input: &str) -> String {
let mut c = input.chars(); let mut c = input.chars();
match c.next() { match c.next() {
None => String::new(), None => String::new(),
Some(first) => first.collect::<String>() + c.as_str(), Some(first) => format!("{}{}", first.to_uppercase(), c.collect::<String>()),
} }
} }
pub fn capitalize_first_vec(vec_str: Vec<&str>) -> Vec<String> {
let mut iter = vec_str.iter();
iter.map(|i| capitalize_first(i)).collect()
}
pub fn capitalize_first_vec_to_str(vec_str: Vec<&str>) -> String {
let mut iter = vec_str.iter();
iter.map(|i| capitalize_first(i)).collect::<String>()
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -37,14 +45,14 @@ mod tests {
#[test] #[test]
fn test_iterate_string_vec() { fn test_iterate_string_vec() {
let words = vec!["hello", "world"]; let words = vec!["hello", "world"];
let capitalized_words: Vec<String> = // TODO let capitalized_words: Vec<String> = capitalize_first_vec(words);
assert_eq!(capitalized_words, ["Hello", "World"]); assert_eq!(capitalized_words, ["Hello", "World"]);
} }
#[test] #[test]
fn test_iterate_into_string() { fn test_iterate_into_string() {
let words = vec!["hello", " ", "world"]; let words = vec!["hello", " ", "world"];
let capitalized_words = // TODO let capitalized_words = capitalize_first_vec_to_str(words);
assert_eq!(capitalized_words, "Hello World"); assert_eq!(capitalized_words, "Hello World");
} }
} }

View file

@ -7,8 +7,6 @@
// Execute `rustlings hint iterators3` to get some hints! // Execute `rustlings hint iterators3` to get some hints!
// Have fun :-) // Have fun :-)
// I AM NOT DONE
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum DivisionError { pub enum DivisionError {
NotDivisible(NotDivisibleError), NotDivisible(NotDivisibleError),
@ -24,7 +22,27 @@ pub struct NotDivisibleError {
// This function should calculate `a` divided by `b` if `a` is // This function should calculate `a` divided by `b` if `a` is
// evenly divisible by b. // evenly divisible by b.
// Otherwise, it should return a suitable error. // Otherwise, it should return a suitable error.
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {} pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
// Both work
// match b {
// 0 => Err(DivisionError::DivideByZero),
// _ => match a % b == 0 {
// true => Ok(a / b),
// false => Err(DivisionError::NotDivisible(NotDivisibleError {
// dividend: a,
// divisor: b,
// })),
// },
// }
match (a, b) {
(_, 0) => Err(DivisionError::DivideByZero),
(a, b) if a % b != 0 => Err(DivisionError::NotDivisible(NotDivisibleError {
dividend: a,
divisor: b,
})),
_ => Ok(a / b),
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -58,21 +76,19 @@ mod tests {
} }
// Iterator exercises using your `divide` function // Iterator exercises using your `divide` function
/*
#[test] #[test]
fn result_with_list() { fn result_with_list() {
let numbers = vec![27, 297, 38502, 81]; let numbers = vec![27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27)); let division_results = numbers.into_iter().map(|n| divide(n, 27));
let x //... Fill in here! let x = division_results.collect::<Result<Vec<i32>, DivisionError>>();
assert_eq!(format!("{:?}", x), "Ok([1, 11, 1426, 3])"); assert_eq!(format!("{:?}", x), "Ok([1, 11, 1426, 3])",);
} }
#[test] #[test]
fn list_of_results() { fn list_of_results() {
let numbers = vec![27, 297, 38502, 81]; let numbers = vec![27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27)); let division_results = numbers.into_iter().map(|n| divide(n, 27));
let x //... Fill in here! let x = division_results.collect::<Vec<Result<i32, DivisionError>>>();
assert_eq!(format!("{:?}", x), "[Ok(1), Ok(11), Ok(1426), Ok(3)]"); assert_eq!(format!("{:?}", x), "[Ok(1), Ok(11), Ok(1426), Ok(3)]");
} }
*/
} }

View file

@ -1,7 +1,5 @@
// iterators4.rs // iterators4.rs
// I AM NOT DONE
pub fn factorial(num: u64) -> u64 { pub fn factorial(num: u64) -> u64 {
// Complete this function to return the factorial of num // Complete this function to return the factorial of num
// Do not use: // Do not use:
@ -12,6 +10,7 @@ pub fn factorial(num: u64) -> u64 {
// For an extra challenge, don't use: // For an extra challenge, don't use:
// - recursion // - recursion
// Execute `rustlings hint iterators4` for hints. // Execute `rustlings hint iterators4` for hints.
(1..=num).product()
} }
#[cfg(test)] #[cfg(test)]