Revert "did some more"

This reverts commit b8bedd224f724c0d3197ac8031be3eb31134b3dc.
This commit is contained in:
Stanislav Pankrashin 2022-06-23 17:06:07 +12:00
parent c695b02d94
commit c90ae094e9
15 changed files with 64 additions and 70 deletions

View File

@ -1,21 +1,23 @@
// option1.rs
// Make me compile! Execute `rustlings hint option1` for hints
// I AM NOT DONE
// you can modify anything EXCEPT for this function's signature
fn print_number(maybe_number: Option<u16>) {
println!("printing: {}", maybe_number.unwrap());
}
fn main() {
print_number(Some(13));
print_number(Some(99));
print_number(13);
print_number(99);
let mut numbers: [Option<u16>; 5] = [Some(0); 5];
let mut numbers: [Option<u16>; 5];
for iter in 0..5 {
let number_to_add: u16 = {
((iter * 1235) + 2) / (4 * 16)
};
numbers[iter as usize] = Some(number_to_add);
numbers[iter as usize] = number_to_add;
}
}

View File

@ -1,14 +1,16 @@
// option2.rs
// Make me compile! Execute `rustlings hint option2` for hints
// I AM NOT DONE
fn main() {
let optional_word = Some(String::from("rustlings"));
// TODO: Make this an if let statement whose value is "Some" type
if let word = optional_word != None {
word = optional_word {
println!("The word is: {}", word);
} else {
println!("The optional word doesn't contain anything");
};
}
let mut optional_integers_vec: Vec<Option<i8>> = Vec::new();
for x in 1..10 {
@ -17,7 +19,7 @@ fn main() {
// TODO: make this a while let statement - remember that vector.pop also adds another layer of Option<T>
// You can stack `Option<T>`'s into while let and if let
while let Some(integer) = optional_integers_vec.pop() {
println!("current value: {:?}", integer);
integer = optional_integers_vec.pop() {
println!("current value: {}", integer);
}
}

View File

@ -1,6 +1,8 @@
// option3.rs
// Make me compile! Execute `rustlings hint option3` for hints
// I AM NOT DONE
struct Point {
x: i32,
y: i32,
@ -10,7 +12,7 @@ fn main() {
let y: Option<Point> = Some(Point { x: 100, y: 200 });
match y {
Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y),
Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y),
_ => println!("no match"),
}
y; // Fix without deleting this line.

View File

@ -7,6 +7,8 @@
// we expect to get when we call `times_two` with a negative number.
// No hints, you can do this :)
// I AM NOT DONE
pub fn times_two(num: i32) -> i32 {
num * 2
}
@ -17,12 +19,12 @@ mod tests {
#[test]
fn returns_twice_of_positive_numbers() {
assert_eq!(times_two(4), 8);
assert_eq!(times_two(4), ???);
}
#[test]
fn returns_twice_of_negative_numbers() {
// TODO replace unimplemented!() with an assert for `times_two(-4)`
assert_eq!(times_two(-4), -8);
unimplemented!()
}
}

View File

@ -18,17 +18,19 @@
// where the second TODO comment is. Try not to create any copies of the `numbers` Vec!
// Execute `rustlings hint arc1` for hints :)
// 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: Arc<Vec<u32>> = Arc::from(numbers);// TODO
let shared_numbers = // TODO
let mut joinhandles = Vec::new();
for offset in 0..8 {
let child_numbers = Arc::clone(&shared_numbers);// TODO
let child_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);

View File

@ -16,9 +16,11 @@
//
// Execute `rustlings hint box1` for hints :)
// I AM NOT DONE
#[derive(PartialEq, Debug)]
pub enum List {
Cons(i32, Box<List>),
Cons(i32, List),
Nil,
}
@ -31,12 +33,11 @@ fn main() {
}
pub fn create_empty_list() -> List {
List::Nil
unimplemented!()
}
pub fn create_non_empty_list() -> List {
use List::Cons;
List::Cons(0, Box::new(Cons(1, Box::new(List::Nil))))
unimplemented!()
}
#[cfg(test)]

View File

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

View File

@ -3,6 +3,8 @@
// can offer. Follow the steps to complete the exercise.
// As always, there are hints if you execute `rustlings hint iterators2`!
// I AM NOT DONE
// Step 1.
// Complete the `capitalize_first` function.
// "hello" -> "Hello"
@ -10,12 +12,7 @@ pub fn capitalize_first(input: &str) -> String {
let mut c = input.chars();
match c.next() {
None => String::new(),
Some(first) => {
let mut new_string = first.to_uppercase().to_string();
let remaining_str = c.as_str();
new_string.push_str(remaining_str);
new_string
}
Some(first) => ???,
}
}
@ -24,12 +21,7 @@ pub fn capitalize_first(input: &str) -> String {
// Return a vector of strings.
// ["hello", "world"] -> ["Hello", "World"]
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
let mut result_vec: Vec<String> = vec![];
for word in words {
result_vec.push(capitalize_first(word));
}
result_vec
vec![]
}
// Step 3.
@ -37,7 +29,7 @@ pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
// Return a single string.
// ["hello", " ", "world"] -> "Hello World"
pub fn capitalize_words_string(words: &[&str]) -> String {
capitalize_words_vector(words).join("")
String::new()
}
#[cfg(test)]

View File

@ -6,6 +6,8 @@
// list_of_results functions.
// Execute `rustlings hint iterators3` to get some hints!
// I AM NOT DONE
#[derive(Debug, PartialEq, Eq)]
pub enum DivisionError {
NotDivisible(NotDivisibleError),
@ -20,31 +22,20 @@ 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> {
if b == 0 {
return Err(DivisionError::DivideByZero)
} else if a % b != 0 {
return Err(DivisionError::NotDivisible(NotDivisibleError {dividend: a, divisor: b}))
}
return Ok(a / b)
}
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {}
// 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() -> Result<Vec<i32>, DivisionError> {
fn result_with_list() -> () {
let numbers = vec![27, 297, 38502, 81];
let division_results: Result<Vec<i32>, DivisionError> = numbers.into_iter().map(|n| divide(n, 27)).collect();
division_results
let division_results = numbers.into_iter().map(|n| divide(n, 27));
}
// 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() -> Vec<Result<i32, DivisionError>> {
fn list_of_results() -> () {
let numbers = vec![27, 297, 38502, 81];
let division_results: Vec<Result<i32, DivisionError>> = numbers.into_iter().map(|n| divide(n, 27)).collect();
division_results
let division_results = numbers.into_iter().map(|n| divide(n, 27));
}
#[cfg(test)]

View File

@ -1,5 +1,7 @@
// iterators4.rs
// I AM NOT DONE
pub fn factorial(num: u64) -> u64 {
// Complete this function to return the factorial of num
// Do not use:
@ -10,10 +12,6 @@ pub fn factorial(num: u64) -> u64 {
// For an extra challenge, don't use:
// - recursion
// Execute `rustlings hint iterators4` for hints.
match (1..num + 1).map(|x| x).reduce(|a, b| a * b) {
Some(result) => result,
None => panic!("something went really wrong"),
}
}
#[cfg(test)]

View File

@ -6,10 +6,12 @@
// This test has a problem with it -- make the test compile! Make the test
// pass! Make the test fail! Execute `rustlings hint tests1` for hints :)
// I AM NOT DONE
#[cfg(test)]
mod tests {
#[test]
fn you_can_assert() {
assert!(true);
assert!();
}
}

View File

@ -2,10 +2,12 @@
// This test has a problem with it -- make the test compile! Make the test
// pass! Make the test fail! Execute `rustlings hint tests2` for hints :)
// I AM NOT DONE
#[cfg(test)]
mod tests {
#[test]
fn you_can_assert_eq() {
assert_eq!(1, 1);
assert_eq!();
}
}

View File

@ -4,6 +4,8 @@
// we expect to get when we call `is_even(5)`.
// Execute `rustlings hint tests3` for hints :)
// I AM NOT DONE
pub fn is_even(num: i32) -> bool {
num % 2 == 0
}
@ -14,11 +16,11 @@ mod tests {
#[test]
fn is_true_when_even() {
assert!(is_even(2));
assert!();
}
#[test]
fn is_false_when_odd() {
assert!(is_even(3) == false);
assert!();
}
}

View File

@ -8,15 +8,14 @@
// which appends "Bar" to any object
// implementing this trait.
// I AM NOT DONE
trait AppendBar {
fn append_bar(self) -> Self;
}
impl AppendBar for String {
fn append_bar(mut self) -> Self {
self.push_str("Bar");
self
}
//Add your code here
}
fn main() {

View File

@ -10,26 +10,21 @@
// No boiler plate code this time,
// you can do this!
// I AM NOT DONE
trait AppendBar {
fn append_bar(self) -> Self;
}
//TODO: Add your code here
impl AppendBar for Vec<String> {
fn append_bar(mut self) -> Self {
self.push(String::from("Bar"));
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_vec_pop_eq_bar() {
let mut foo: Vec<String> = vec![String::from("Foo")].append_bar();
let mut foo = vec![String::from("Foo")].append_bar();
assert_eq!(foo.pop().unwrap(), String::from("Bar"));
assert_eq!(foo.pop().unwrap(), String::from("Foo"));
}