control flow, primitive types and vectors
This commit is contained in:
@@ -4,10 +4,13 @@ fn bigger(a: i32, b: i32) -> i32 {
|
|||||||
// Do not use:
|
// Do not use:
|
||||||
// - another function call
|
// - another function call
|
||||||
// - additional variables
|
// - additional variables
|
||||||
|
|
||||||
|
if a > b { a } else { b }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// You can optionally experiment here.
|
// You can optionally experiment here.
|
||||||
|
println!("{}", bigger(32, 42));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't mind this for now :)
|
// Don't mind this for now :)
|
||||||
|
|||||||
@@ -2,8 +2,10 @@
|
|||||||
fn picky_eater(food: &str) -> &str {
|
fn picky_eater(food: &str) -> &str {
|
||||||
if food == "strawberry" {
|
if food == "strawberry" {
|
||||||
"Yummy!"
|
"Yummy!"
|
||||||
|
} else if food == "potato" {
|
||||||
|
"I guess I can eat that."
|
||||||
} else {
|
} else {
|
||||||
1
|
"No thanks!"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ fn animal_habitat(animal: &str) -> &str {
|
|||||||
let identifier = if animal == "crab" {
|
let identifier = if animal == "crab" {
|
||||||
1
|
1
|
||||||
} else if animal == "gopher" {
|
} else if animal == "gopher" {
|
||||||
2.0
|
2
|
||||||
} else if animal == "snake" {
|
} else if animal == "snake" {
|
||||||
3
|
3
|
||||||
} else {
|
} else {
|
||||||
"Unknown"
|
0
|
||||||
};
|
};
|
||||||
|
|
||||||
// Don't change the expression below!
|
// Don't change the expression below!
|
||||||
|
|||||||
@@ -8,7 +8,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 is_evening = !is_morning;
|
||||||
if is_evening {
|
if is_evening {
|
||||||
println!("Good evening!");
|
println!("Good evening!");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
// Note the _single_ quotes, these are different from the double quotes
|
// Note the _single_ quotes, these are different from the double quotes
|
||||||
// you've been seeing around.
|
// you've been seeing around.
|
||||||
|
|
||||||
let my_first_initial = 'C';
|
let my_first_initial = 'C';
|
||||||
if my_first_initial.is_alphabetic() {
|
if my_first_initial.is_alphabetic() {
|
||||||
println!("Alphabetical!");
|
println!("Alphabetical!");
|
||||||
@@ -16,7 +17,8 @@ fn main() {
|
|||||||
// below with your favorite character.
|
// below with your favorite character.
|
||||||
// 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 = '4';
|
||||||
|
|
||||||
if your_character.is_alphabetic() {
|
if your_character.is_alphabetic() {
|
||||||
println!("Alphabetical!");
|
println!("Alphabetical!");
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
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 = [0; 100];
|
||||||
|
|
||||||
if a.len() >= 100 {
|
if a.len() >= 100 {
|
||||||
println!("Wow, that's a big array!");
|
println!("Wow, that's a big array!");
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ mod tests {
|
|||||||
let a = [1, 2, 3, 4, 5];
|
let a = [1, 2, 3, 4, 5];
|
||||||
|
|
||||||
// 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 = &a[1..4];
|
||||||
|
|
||||||
assert_eq!([2, 3, 4], nice_slice);
|
assert_eq!([2, 3, 4], nice_slice);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,5 +4,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");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,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 = 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!");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,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 = vec![10, 20, 30, 40];
|
||||||
|
|
||||||
(a, v)
|
(a, v)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -21,12 +22,7 @@ fn vec_map(input: &[i32]) -> Vec<i32> {
|
|||||||
// by 2, but with iterator mapping instead of manually pushing into an empty
|
// by 2, but with iterator mapping instead of manually pushing into an empty
|
||||||
// vector.
|
// vector.
|
||||||
// See the example in the function `vec_map_example` above.
|
// See the example in the function `vec_map_example` above.
|
||||||
input
|
input.iter().map(|element| element * 2).collect()
|
||||||
.iter()
|
|
||||||
.map(|element| {
|
|
||||||
// ???
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|
||||||
|
|||||||
@@ -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]);
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ mod tests {
|
|||||||
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]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,12 @@
|
|||||||
// removing references (the character `&`).
|
// removing references (the character `&`).
|
||||||
|
|
||||||
// Shouldn't take ownership
|
// Shouldn't take ownership
|
||||||
fn get_char(data: String) -> char {
|
fn get_char(data: &String) -> char {
|
||||||
data.chars().last().unwrap()
|
data.chars().last().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
||||||
string_uppercase(&data);
|
string_uppercase(data);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
struct ColorRegularStruct {
|
struct ColorRegularStruct {
|
||||||
// TODO: Add the fields that the test `regular_structs` expects.
|
// TODO: Add the fields that the test `regular_structs` expects.
|
||||||
// What types should the fields have? What are the minimum and maximum values for RGB colors?
|
// What types should the fields have? What are the minimum and maximum values for RGB colors?
|
||||||
|
red: u16,
|
||||||
|
green: u16,
|
||||||
|
blue: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ColorTupleStruct(/* TODO: Add the fields that the test `tuple_structs` expects */);
|
struct ColorTupleStruct(u16, u16, u16);
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct UnitStruct;
|
struct UnitStruct;
|
||||||
@@ -19,7 +22,11 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn regular_structs() {
|
fn regular_structs() {
|
||||||
// TODO: Instantiate a regular struct.
|
// TODO: Instantiate a regular struct.
|
||||||
// let green =
|
let green = ColorRegularStruct {
|
||||||
|
red: 0,
|
||||||
|
green: 255,
|
||||||
|
blue: 0,
|
||||||
|
};
|
||||||
|
|
||||||
assert_eq!(green.red, 0);
|
assert_eq!(green.red, 0);
|
||||||
assert_eq!(green.green, 255);
|
assert_eq!(green.green, 255);
|
||||||
@@ -29,7 +36,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn tuple_structs() {
|
fn tuple_structs() {
|
||||||
// TODO: Instantiate a tuple struct.
|
// TODO: Instantiate a tuple struct.
|
||||||
// let green =
|
let green = ColorTupleStruct(0, 255, 0);
|
||||||
|
|
||||||
assert_eq!(green.0, 0);
|
assert_eq!(green.0, 0);
|
||||||
assert_eq!(green.1, 255);
|
assert_eq!(green.1, 255);
|
||||||
@@ -39,7 +46,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn unit_structs() {
|
fn unit_structs() {
|
||||||
// TODO: Instantiate a unit struct.
|
// TODO: Instantiate a unit struct.
|
||||||
// let unit_struct =
|
let unit_struct = UnitStruct;
|
||||||
let message = format!("{unit_struct:?}s are fun!");
|
let message = format!("{unit_struct:?}s are fun!");
|
||||||
|
|
||||||
assert_eq!(message, "UnitStructs are fun!");
|
assert_eq!(message, "UnitStructs are fun!");
|
||||||
|
|||||||
@@ -34,7 +34,13 @@ mod tests {
|
|||||||
let order_template = create_order_template();
|
let order_template = create_order_template();
|
||||||
|
|
||||||
// TODO: Create your own order using the update syntax and template above!
|
// TODO: Create your own order using the update syntax and template above!
|
||||||
// let your_order =
|
let your_order = Order {
|
||||||
|
name: String::from("Hacker in Rust"),
|
||||||
|
year: 2019,
|
||||||
|
item_number: 123,
|
||||||
|
count: 1,
|
||||||
|
..order_template
|
||||||
|
};
|
||||||
|
|
||||||
assert_eq!(your_order.name, "Hacker in Rust");
|
assert_eq!(your_order.name, "Hacker in Rust");
|
||||||
assert_eq!(your_order.year, order_template.year);
|
assert_eq!(your_order.year, order_template.year);
|
||||||
|
|||||||
@@ -12,6 +12,10 @@
|
|||||||
// the quantity bought.
|
// the quantity bought.
|
||||||
// fn calculate_price_of_apples(???) -> ??? { ??? }
|
// fn calculate_price_of_apples(???) -> ??? { ??? }
|
||||||
|
|
||||||
|
fn calculate_price_of_apples(qty: u32) -> u32 {
|
||||||
|
if qty < 41 { qty * 2 } else { qty }
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// You can optionally experiment here.
|
// You can optionally experiment here.
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user