Compare commits

...
3 Commits
Author SHA1 Message Date
AramJonghu 88b981b50c feat: progress iterators 2026-09-13 02:04:29 +02:00
AramJonghu 5c1324aae8 feat: completed tests 2026-09-13 01:13:24 +02:00
AramJonghu c7f0365fec feat: completed lifetimes 2026-09-13 01:13:13 +02:00
8 changed files with 21 additions and 18 deletions
+1 -1
View File
@@ -4,7 +4,7 @@
// not own their own data. What if their owner goes out of scope?
// 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() {
x
} else {
+1 -1
View File
@@ -15,6 +15,6 @@ fn main() {
{
let string2 = String::from("xyz");
result = longest(&string1, &string2);
println!("The longest string is '{result}'");
}
println!("The longest string is '{result}'");
}
+3 -3
View File
@@ -1,9 +1,9 @@
// Lifetimes are also needed when structs hold references.
// TODO: Fix the compiler errors about the struct.
struct Book {
author: &str,
title: &str,
struct Book<'a> {
author: &'a str,
title: &'a str,
}
fn main() {
+3 -2
View File
@@ -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::*;
#[test]
fn you_can_assert() {
// TODO: Test the function `is_even` with some values.
assert!();
assert!();
assert!(!is_even(1));
assert!(is_even(12));
}
}
+4 -4
View File
@@ -15,9 +15,9 @@ 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(10), 1024);
assert_eq!(power_of_2(42), 4398046511104);
}
}
+4 -2
View File
@@ -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);
}
+4 -4
View File
@@ -13,13 +13,13 @@ mod tests {
let my_fav_fruits = &["banana", "custard apple", "avocado", "peach", "raspberry"];
// 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(), 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!()`
}
}
+1 -1
View File
@@ -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) => char::to_uppercase(first).to_string(),
}
}