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:
|
||||
// - another function call
|
||||
// - additional variables
|
||||
|
||||
if a > b { a } else { b }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
println!("{}", bigger(32, 42));
|
||||
}
|
||||
|
||||
// Don't mind this for now :)
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
fn picky_eater(food: &str) -> &str {
|
||||
if food == "strawberry" {
|
||||
"Yummy!"
|
||||
} else if food == "potato" {
|
||||
"I guess I can eat that."
|
||||
} else {
|
||||
1
|
||||
"No thanks!"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,11 +3,11 @@ fn animal_habitat(animal: &str) -> &str {
|
||||
let identifier = if animal == "crab" {
|
||||
1
|
||||
} else if animal == "gopher" {
|
||||
2.0
|
||||
2
|
||||
} else if animal == "snake" {
|
||||
3
|
||||
} else {
|
||||
"Unknown"
|
||||
0
|
||||
};
|
||||
|
||||
// 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.
|
||||
// The value of the variable should be the negation (opposite) of `is_morning`.
|
||||
// let …
|
||||
let is_evening = !is_morning;
|
||||
if is_evening {
|
||||
println!("Good evening!");
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
fn main() {
|
||||
// Note the _single_ quotes, these are different from the double quotes
|
||||
// you've been seeing around.
|
||||
|
||||
let my_first_initial = 'C';
|
||||
if my_first_initial.is_alphabetic() {
|
||||
println!("Alphabetical!");
|
||||
@@ -16,7 +17,8 @@ fn main() {
|
||||
// below with your favorite 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 😉
|
||||
// let your_character = '';
|
||||
|
||||
let your_character = '4';
|
||||
|
||||
if your_character.is_alphabetic() {
|
||||
println!("Alphabetical!");
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
fn main() {
|
||||
// TODO: Create an array called `a` with at least 100 elements in it.
|
||||
// let a = ???
|
||||
let a = [0; 100];
|
||||
|
||||
if a.len() >= 100 {
|
||||
println!("Wow, that's a big array!");
|
||||
|
||||
@@ -9,7 +9,7 @@ mod tests {
|
||||
let a = [1, 2, 3, 4, 5];
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
@@ -4,5 +4,7 @@ fn main() {
|
||||
// TODO: Destructure the `cat` tuple in one statement so that the println works.
|
||||
// let /* your pattern here */ = cat;
|
||||
|
||||
let (name, age) = cat;
|
||||
|
||||
println!("{name} is {age} years old");
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ mod tests {
|
||||
|
||||
// TODO: Use a tuple index to access the second element of `numbers`
|
||||
// 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!");
|
||||
}
|
||||
|
||||
@@ -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`.
|
||||
// Use the vector macro.
|
||||
// let v = ???;
|
||||
let v = vec![10, 20, 30, 40];
|
||||
|
||||
(a, v)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ fn vec_loop(input: &[i32]) -> Vec<i32> {
|
||||
for element in input {
|
||||
// TODO: Multiply each element in the `input` slice by 2 and push it to
|
||||
// the `output` vector.
|
||||
output.push(*element * 2);
|
||||
}
|
||||
|
||||
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
|
||||
// vector.
|
||||
// See the example in the function `vec_map_example` above.
|
||||
input
|
||||
.iter()
|
||||
.map(|element| {
|
||||
// ???
|
||||
})
|
||||
.collect()
|
||||
input.iter().map(|element| element * 2).collect()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// TODO: Fix the compiler error in this function.
|
||||
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||
let vec = vec;
|
||||
let mut vec = vec;
|
||||
|
||||
vec.push(88);
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ mod tests {
|
||||
fn move_semantics2() {
|
||||
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!(vec1, [22, 44, 66, 88]);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// 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
|
||||
|
||||
@@ -10,8 +10,8 @@ mod tests {
|
||||
fn move_semantics4() {
|
||||
let mut x = Vec::new();
|
||||
let y = &mut x;
|
||||
let z = &mut x;
|
||||
y.push(42);
|
||||
let z = &mut x;
|
||||
z.push(13);
|
||||
assert_eq!(x, [42, 13]);
|
||||
}
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
// removing references (the character `&`).
|
||||
|
||||
// Shouldn't take ownership
|
||||
fn get_char(data: String) -> char {
|
||||
fn get_char(data: &String) -> char {
|
||||
data.chars().last().unwrap()
|
||||
}
|
||||
|
||||
// Should take ownership
|
||||
fn string_uppercase(mut data: &String) {
|
||||
fn string_uppercase(mut data: String) {
|
||||
data = data.to_uppercase();
|
||||
|
||||
println!("{data}");
|
||||
@@ -18,7 +18,7 @@ fn string_uppercase(mut data: &String) {
|
||||
fn main() {
|
||||
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 {
|
||||
// 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?
|
||||
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)]
|
||||
struct UnitStruct;
|
||||
@@ -19,7 +22,11 @@ mod tests {
|
||||
#[test]
|
||||
fn regular_structs() {
|
||||
// TODO: Instantiate a regular struct.
|
||||
// let green =
|
||||
let green = ColorRegularStruct {
|
||||
red: 0,
|
||||
green: 255,
|
||||
blue: 0,
|
||||
};
|
||||
|
||||
assert_eq!(green.red, 0);
|
||||
assert_eq!(green.green, 255);
|
||||
@@ -29,7 +36,7 @@ mod tests {
|
||||
#[test]
|
||||
fn tuple_structs() {
|
||||
// TODO: Instantiate a tuple struct.
|
||||
// let green =
|
||||
let green = ColorTupleStruct(0, 255, 0);
|
||||
|
||||
assert_eq!(green.0, 0);
|
||||
assert_eq!(green.1, 255);
|
||||
@@ -39,7 +46,7 @@ mod tests {
|
||||
#[test]
|
||||
fn unit_structs() {
|
||||
// TODO: Instantiate a unit struct.
|
||||
// let unit_struct =
|
||||
let unit_struct = UnitStruct;
|
||||
let message = format!("{unit_struct:?}s are fun!");
|
||||
|
||||
assert_eq!(message, "UnitStructs are fun!");
|
||||
|
||||
@@ -34,7 +34,13 @@ mod tests {
|
||||
let order_template = create_order_template();
|
||||
|
||||
// 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.year, order_template.year);
|
||||
|
||||
@@ -12,6 +12,10 @@
|
||||
// the quantity bought.
|
||||
// fn calculate_price_of_apples(???) -> ??? { ??? }
|
||||
|
||||
fn calculate_price_of_apples(qty: u32) -> u32 {
|
||||
if qty < 41 { qty * 2 } else { qty }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user