mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-28 06:49:19 +00:00
study 4-6
This commit is contained in:
parent
a2e9d3cae7
commit
17f5f39e51
@ -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: bool = false;
|
||||
if is_evening {
|
||||
println!("Good evening!");
|
||||
}
|
||||
|
||||
@ -16,7 +16,7 @@ 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 = '1';
|
||||
|
||||
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 = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ fn main() {
|
||||
let cat = ("Furry McFurson", 3.5);
|
||||
|
||||
// TODO: Destructure the `cat` tuple in one statement so that the println works.
|
||||
// let /* your pattern here */ = cat;
|
||||
let (name, age): (&str, f32) = 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: i32 = 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 = a.to_vec();
|
||||
|
||||
(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
|
||||
@ -24,7 +25,7 @@ fn vec_map(input: &[i32]) -> Vec<i32> {
|
||||
input
|
||||
.iter()
|
||||
.map(|element| {
|
||||
// ???
|
||||
element * 2
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
@ -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 = 100;
|
||||
let y = &mut x;
|
||||
let z = &mut x;
|
||||
*y += 100;
|
||||
let z = &mut x;
|
||||
*z += 1000;
|
||||
assert_eq!(x, 1200);
|
||||
}
|
||||
|
||||
@ -6,18 +6,18 @@
|
||||
fn main() {
|
||||
let data = "Rust is great!".to_string();
|
||||
|
||||
get_char(data);
|
||||
get_char(&data);
|
||||
|
||||
string_uppercase(&data);
|
||||
string_uppercase(data);
|
||||
}
|
||||
|
||||
// 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}");
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
|
||||
// TODO: Write a function that calculates the price of an order of apples given
|
||||
// the quantity bought.
|
||||
// fn calculate_price_of_apples(???) -> ??? { ??? }
|
||||
fn calculate_price_of_apples(num: i32) -> i32 { if num <= 40 { num * 2 } else { num } }
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user