mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-03-31 03:29:19 +00:00
4 to 6 excercises
This commit is contained in:
parent
56b04dbc4d
commit
b2f3f6865a
@ -9,6 +9,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!");
|
||||
}
|
||||
|
||||
@ -17,7 +17,7 @@ fn main() {
|
||||
// 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 = '😊';
|
||||
if your_character.is_alphabetic() {
|
||||
println!("Alphabetical!");
|
||||
} else if your_character.is_numeric() {
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
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!");
|
||||
} else {
|
||||
|
||||
@ -10,6 +10,7 @@ mod tests {
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
@ -3,6 +3,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");
|
||||
}
|
||||
|
||||
@ -11,6 +11,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!");
|
||||
}
|
||||
|
||||
@ -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`.
|
||||
// Use the vector macro.
|
||||
// let v = ???;
|
||||
let v = vec![10, 20, 30, 40]; // Vector
|
||||
|
||||
(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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -9,10 +9,13 @@ mod tests {
|
||||
#[test]
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,7 +9,7 @@ fn get_char(data: String) -> char {
|
||||
}
|
||||
|
||||
// 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.clone());
|
||||
|
||||
string_uppercase(&data);
|
||||
string_uppercase(data);
|
||||
}
|
||||
|
||||
@ -12,6 +12,14 @@
|
||||
// the quantity bought.
|
||||
// fn calculate_price_of_apples(???) -> ??? { ??? }
|
||||
|
||||
fn calculate_price_of_apples(quantity: i32) -> i32 {
|
||||
if quantity > 40 {
|
||||
quantity
|
||||
} else {
|
||||
quantity * 2
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user