mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-28 06:49:19 +00:00
coding rust 17-18
This commit is contained in:
parent
f3862cb467
commit
408e9f2807
@ -13,11 +13,12 @@ fn main() {
|
||||
mod tests {
|
||||
// TODO: Import `is_even`. You can use a wildcard to import everything in
|
||||
// the outer module.
|
||||
use super::is_even;
|
||||
|
||||
#[test]
|
||||
fn you_can_assert() {
|
||||
// TODO: Test the function `is_even` with some values.
|
||||
assert!();
|
||||
assert!();
|
||||
assert!(is_even(10));
|
||||
assert!(!is_even(11));
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,10 +14,11 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn you_can_assert_eq() {
|
||||
|
||||
// TODO: Test the function `power_of_2` with some values.
|
||||
assert_eq!();
|
||||
assert_eq!();
|
||||
assert_eq!();
|
||||
assert_eq!();
|
||||
assert_eq!(power_of_2(2), 4);
|
||||
assert_eq!(power_of_2(3), 8);
|
||||
assert_eq!(power_of_2(4), 16);
|
||||
assert_eq!(power_of_2(8), 256);
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,13 +29,14 @@ mod tests {
|
||||
// TODO: This test should check if the rectangle has the size that we
|
||||
// pass to its constructor.
|
||||
let rect = Rectangle::new(10, 20);
|
||||
assert_eq!(todo!(), 10); // Check width
|
||||
assert_eq!(todo!(), 20); // Check height
|
||||
assert_eq!(rect.width, 10); // Check width
|
||||
assert_eq!(rect.height, 20); // Check height
|
||||
}
|
||||
|
||||
// TODO: This test should check if the program panics when we try to create
|
||||
// a rectangle with negative width.
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn negative_width() {
|
||||
let _rect = Rectangle::new(-10, 10);
|
||||
}
|
||||
@ -43,6 +44,7 @@ mod tests {
|
||||
// TODO: This test should check if the program panics when we try to create
|
||||
// a rectangle with negative height.
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn negative_height() {
|
||||
let _rect = Rectangle::new(10, -10);
|
||||
}
|
||||
|
||||
@ -13,13 +13,13 @@ mod tests {
|
||||
let my_fav_fruits = ["banana", "custard apple", "avocado", "peach", "raspberry"];
|
||||
|
||||
// TODO: Create an iterator over the array.
|
||||
let mut fav_fruits_iterator = todo!();
|
||||
let mut fav_fruits_iterator = my_fav_fruits.iter();
|
||||
|
||||
assert_eq!(fav_fruits_iterator.next(), Some(&"banana"));
|
||||
assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
|
||||
assert_eq!(fav_fruits_iterator.next(), Some(&"custard apple")); // TODO: Replace `todo!()`
|
||||
assert_eq!(fav_fruits_iterator.next(), Some(&"avocado"));
|
||||
assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
|
||||
assert_eq!(fav_fruits_iterator.next(), Some(&"peach")); // TODO: Replace `todo!()`
|
||||
assert_eq!(fav_fruits_iterator.next(), Some(&"raspberry"));
|
||||
assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
|
||||
assert_eq!(fav_fruits_iterator.next(), None); // TODO: Replace `todo!()`
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@ fn capitalize_first(input: &str) -> String {
|
||||
let mut chars = input.chars();
|
||||
match chars.next() {
|
||||
None => String::new(),
|
||||
Some(first) => todo!(),
|
||||
Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,14 +15,20 @@ fn capitalize_first(input: &str) -> String {
|
||||
// Return a vector of strings.
|
||||
// ["hello", "world"] -> ["Hello", "World"]
|
||||
fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
||||
// ???
|
||||
words.iter().map(|x| {
|
||||
capitalize_first(x)
|
||||
}).collect()
|
||||
}
|
||||
|
||||
// TODO: Apply the `capitalize_first` function again to a slice of string
|
||||
// slices. Return a single string.
|
||||
// ["hello", " ", "world"] -> "Hello World"
|
||||
fn capitalize_words_string(words: &[&str]) -> String {
|
||||
// ???
|
||||
let mut string = String::new();
|
||||
words.iter().for_each(|x| {
|
||||
string.push_str(capitalize_first(x).as_str());
|
||||
});
|
||||
string
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@ -7,21 +7,27 @@ enum DivisionError {
|
||||
// TODO: Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
||||
// Otherwise, return a suitable error.
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Add the correct return type and complete the function body.
|
||||
// Desired output: `Ok([1, 11, 1426, 3])`
|
||||
fn result_with_list() {
|
||||
fn result_with_list() -> Result<Vec<i32>, DivisionError> {
|
||||
let numbers = [27, 297, 38502, 81];
|
||||
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
||||
numbers.into_iter().map(|n| divide(n, 27)).collect()
|
||||
}
|
||||
|
||||
// TODO: Add the correct return type and complete the function body.
|
||||
// Desired output: `[Ok(1), Ok(11), Ok(1426), Ok(3)]`
|
||||
fn list_of_results() {
|
||||
fn list_of_results() -> Vec<Result<i32, DivisionError>> {
|
||||
let numbers = [27, 297, 38502, 81];
|
||||
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
||||
numbers.into_iter().map(|n| divide(n, 27)).collect()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@ -7,6 +7,7 @@ fn factorial(num: u64) -> u64 {
|
||||
// - additional variables
|
||||
// For an extra challenge, don't use:
|
||||
// - recursion
|
||||
(1..=num).product()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@ -28,6 +28,7 @@ fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
||||
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
||||
// `map` is a hash map with `String` keys and `Progress` values.
|
||||
// map = { "variables1": Complete, "from_str": None, … }
|
||||
map.iter().filter(|x| { x.1 == &value }).count()
|
||||
}
|
||||
|
||||
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
||||
@ -48,6 +49,7 @@ fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Pr
|
||||
// `collection` is a slice of hash maps.
|
||||
// collection = [{ "variables1": Complete, "from_str": None, … },
|
||||
// { "variables2": Complete, … }, … ]
|
||||
collection.iter().map(|x| { count_iterator(x, value) }).sum()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user