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