mirror of
https://git.aramjonghu.dev/AramJonghu/rustlings.git
synced 2026-10-30 15:06:29 +01:00
Compare commits
4
Commits
3f32906547
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
50f23f7e48
|
||
|
|
88b981b50c
|
||
|
|
5c1324aae8
|
||
|
|
c7f0365fec
|
@@ -4,7 +4,7 @@
|
|||||||
// not own their own data. What if their owner goes out of scope?
|
// not own their own data. What if their owner goes out of scope?
|
||||||
|
|
||||||
// TODO: Fix the compiler error by updating the function signature.
|
// TODO: Fix the compiler error by updating the function signature.
|
||||||
fn longest(x: &str, y: &str) -> &str {
|
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
||||||
if x.len() > y.len() {
|
if x.len() > y.len() {
|
||||||
x
|
x
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -15,6 +15,6 @@ fn main() {
|
|||||||
{
|
{
|
||||||
let string2 = String::from("xyz");
|
let string2 = String::from("xyz");
|
||||||
result = longest(&string1, &string2);
|
result = longest(&string1, &string2);
|
||||||
|
println!("The longest string is '{result}'");
|
||||||
}
|
}
|
||||||
println!("The longest string is '{result}'");
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
// Lifetimes are also needed when structs hold references.
|
// Lifetimes are also needed when structs hold references.
|
||||||
|
|
||||||
// TODO: Fix the compiler errors about the struct.
|
// TODO: Fix the compiler errors about the struct.
|
||||||
struct Book {
|
struct Book<'a> {
|
||||||
author: &str,
|
author: &'a str,
|
||||||
title: &str,
|
title: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@@ -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::*;
|
||||||
|
|
||||||
#[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(1));
|
||||||
assert!();
|
assert!(is_even(12));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ 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(10), 1024);
|
||||||
assert_eq!();
|
assert_eq!(power_of_2(42), 4398046511104);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 slice.
|
// TODO: Create an iterator over the slice.
|
||||||
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!()`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
// In this exercise, you'll learn some of the unique advantages that iterators
|
// In this exercise, you'll learn some of the unique advantages that iterators
|
||||||
// can offer.
|
// can offer.
|
||||||
|
|
||||||
|
// I failed.
|
||||||
|
|
||||||
// TODO: Complete the `capitalize_first` function.
|
// TODO: Complete the `capitalize_first` function.
|
||||||
// "hello" -> "Hello"
|
// "hello" -> "Hello"
|
||||||
fn capitalize_first(input: &str) -> String {
|
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().to_string() + chars.as_str(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16,6 +18,7 @@ fn capitalize_first(input: &str) -> String {
|
|||||||
// ["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(|word| capitalize_first(word)).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
|
||||||
@@ -23,6 +26,7 @@ fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
|||||||
// ["hello", " ", "world"] -> "Hello World"
|
// ["hello", " ", "world"] -> "Hello World"
|
||||||
fn capitalize_words_string(words: &[&str]) -> String {
|
fn capitalize_words_string(words: &[&str]) -> String {
|
||||||
// ???
|
// ???
|
||||||
|
words.iter().map(|word| capitalize_first(word)).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@@ -11,21 +11,31 @@ 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: i64, b: i64) -> Result<i64, DivisionError> {
|
fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
|
||||||
todo!();
|
if b == 0 {
|
||||||
|
Err(DivisionError::DivideByZero)
|
||||||
|
} else if b == -1 && a == i64::MIN {
|
||||||
|
Err(DivisionError::IntegerOverflow)
|
||||||
|
} 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<i64>, DivisionError> {
|
||||||
let numbers = [27, 297, 38502, 81];
|
let numbers = [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));
|
||||||
|
division_results.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<i64, DivisionError>> {
|
||||||
let numbers = [27, 297, 38502, 81];
|
let numbers = [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));
|
||||||
|
division_results.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ 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() {
|
||||||
|
|||||||
Reference in New Issue
Block a user