4 to 6 excercises

This commit is contained in:
Jorge Esparza 2026-02-21 01:30:48 -06:00
parent 56b04dbc4d
commit b2f3f6865a
14 changed files with 26 additions and 9 deletions

View File

@ -9,6 +9,7 @@ fn main() {
// TODO: Define a boolean variable with the name `is_evening` before the `if` statement below. // TODO: Define a boolean variable with the name `is_evening` before the `if` statement below.
// The value of the variable should be the negation (opposite) of `is_morning`. // The value of the variable should be the negation (opposite) of `is_morning`.
// let … // let …
let is_evening = !is_morning;
if is_evening { if is_evening {
println!("Good evening!"); println!("Good evening!");
} }

View File

@ -17,7 +17,7 @@ fn main() {
// Try a letter, try a digit (in single quotes), try a special character, try a character // Try a letter, try a digit (in single quotes), try a special character, try a character
// from a different language than your own, try an emoji 😉 // from a different language than your own, try an emoji 😉
// let your_character = ''; // let your_character = '';
let your_character = '😊';
if your_character.is_alphabetic() { if your_character.is_alphabetic() {
println!("Alphabetical!"); println!("Alphabetical!");
} else if your_character.is_numeric() { } else if your_character.is_numeric() {

View File

@ -1,7 +1,7 @@
fn main() { fn main() {
// TODO: Create an array called `a` with at least 100 elements in it. // TODO: Create an array called `a` with at least 100 elements in it.
// let a = ??? // let a = ???
let a = [0; 100];
if a.len() >= 100 { if a.len() >= 100 {
println!("Wow, that's a big array!"); println!("Wow, that's a big array!");
} else { } else {

View File

@ -10,6 +10,7 @@ mod tests {
// TODO: Get a slice called `nice_slice` out of the array `a` so that the test passes. // TODO: Get a slice called `nice_slice` out of the array `a` so that the test passes.
// let nice_slice = ??? // let nice_slice = ???
let nice_slice = &a[1..4];
assert_eq!([2, 3, 4], nice_slice); assert_eq!([2, 3, 4], nice_slice);
} }

View File

@ -3,6 +3,7 @@ fn main() {
// TODO: Destructure the `cat` tuple in one statement so that the println works. // TODO: Destructure the `cat` tuple in one statement so that the println works.
// let /* your pattern here */ = cat; // let /* your pattern here */ = cat;
let (name, age) = cat;
println!("{name} is {age} years old"); println!("{name} is {age} years old");
} }

View File

@ -11,6 +11,7 @@ mod tests {
// TODO: Use a tuple index to access the second element of `numbers` // TODO: Use a tuple index to access the second element of `numbers`
// and assign it to a variable called `second`. // and assign it to a variable called `second`.
// let second = ???; // let second = ???;
let second = numbers.1;
assert_eq!(second, 2, "This is not the 2nd number in the tuple!"); assert_eq!(second, 2, "This is not the 2nd number in the tuple!");
} }

View File

@ -4,6 +4,7 @@ fn array_and_vec() -> ([i32; 4], Vec<i32>) {
// TODO: Create a vector called `v` which contains the exact same elements as in the array `a`. // TODO: Create a vector called `v` which contains the exact same elements as in the array `a`.
// Use the vector macro. // Use the vector macro.
// let v = ???; // let v = ???;
let v = vec![10, 20, 30, 40]; // Vector
(a, v) (a, v)
} }

View File

@ -4,6 +4,7 @@ fn vec_loop(input: &[i32]) -> Vec<i32> {
for element in input { for element in input {
// TODO: Multiply each element in the `input` slice by 2 and push it to // TODO: Multiply each element in the `input` slice by 2 and push it to
// the `output` vector. // the `output` vector.
output.push(element * 2);
} }
output output

View File

@ -1,6 +1,6 @@
// TODO: Fix the compiler error in this function. // TODO: Fix the compiler error in this function.
fn fill_vec(vec: Vec<i32>) -> Vec<i32> { fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let vec = vec; let mut vec = vec;
vec.push(88); vec.push(88);

View File

@ -20,7 +20,7 @@ mod tests {
fn move_semantics2() { fn move_semantics2() {
let vec0 = vec![22, 44, 66]; let vec0 = vec![22, 44, 66];
let vec1 = fill_vec(vec0); let vec1 = fill_vec(vec0.clone());
assert_eq!(vec0, [22, 44, 66]); assert_eq!(vec0, [22, 44, 66]);
assert_eq!(vec1, [22, 44, 66, 88]); assert_eq!(vec1, [22, 44, 66, 88]);

View File

@ -1,5 +1,5 @@
// TODO: Fix the compiler error in the function without adding any new line. // TODO: Fix the compiler error in the function without adding any new line.
fn fill_vec(vec: Vec<i32>) -> Vec<i32> { fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
vec.push(88); vec.push(88);
vec vec

View File

@ -9,10 +9,13 @@ mod tests {
#[test] #[test]
fn move_semantics4() { fn move_semantics4() {
let mut x = Vec::new(); let mut x = Vec::new();
let y = &mut x; let y = &mut x;
let z = &mut x;
y.push(42); y.push(42);
let z = &mut x;
z.push(13); z.push(13);
assert_eq!(x, [42, 13]); assert_eq!(x, [42, 13]);
} }
} }

View File

@ -9,7 +9,7 @@ fn get_char(data: String) -> char {
} }
// Should take ownership // Should take ownership
fn string_uppercase(mut data: &String) { fn string_uppercase(mut data: String) {
data = data.to_uppercase(); data = data.to_uppercase();
println!("{data}"); println!("{data}");
@ -18,7 +18,7 @@ fn string_uppercase(mut data: &String) {
fn main() { fn main() {
let data = "Rust is great!".to_string(); let data = "Rust is great!".to_string();
get_char(data); get_char(data.clone());
string_uppercase(&data); string_uppercase(data);
} }

View File

@ -12,6 +12,14 @@
// the quantity bought. // the quantity bought.
// fn calculate_price_of_apples(???) -> ??? { ??? } // fn calculate_price_of_apples(???) -> ??? { ??? }
fn calculate_price_of_apples(quantity: i32) -> i32 {
if quantity > 40 {
quantity
} else {
quantity * 2
}
}
fn main() { fn main() {
// You can optionally experiment here. // You can optionally experiment here.
} }