mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-01 16:29:18 +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.
|
// 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: bool = false;
|
||||||
if is_evening {
|
if is_evening {
|
||||||
println!("Good evening!");
|
println!("Good evening!");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,7 +16,7 @@ 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 = '1';
|
||||||
|
|
||||||
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 = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@ fn main() {
|
|||||||
let cat = ("Furry McFurson", 3.5);
|
let cat = ("Furry McFurson", 3.5);
|
||||||
|
|
||||||
// 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 (name, age): (&str, f32) = 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: i32 = 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 = a.to_vec();
|
||||||
|
|
||||||
(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
|
||||||
@ -24,7 +25,7 @@ fn vec_map(input: &[i32]) -> Vec<i32> {
|
|||||||
input
|
input
|
||||||
.iter()
|
.iter()
|
||||||
.map(|element| {
|
.map(|element| {
|
||||||
// ???
|
element * 2
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 = 100;
|
let mut x = 100;
|
||||||
let y = &mut x;
|
let y = &mut x;
|
||||||
let z = &mut x;
|
|
||||||
*y += 100;
|
*y += 100;
|
||||||
|
let z = &mut x;
|
||||||
*z += 1000;
|
*z += 1000;
|
||||||
assert_eq!(x, 1200);
|
assert_eq!(x, 1200);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,18 +6,18 @@
|
|||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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}");
|
||||||
|
|||||||
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
// TODO: Write a function that calculates the price of an order of apples given
|
// TODO: Write a function that calculates the price of an order of apples given
|
||||||
// the quantity bought.
|
// 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() {
|
fn main() {
|
||||||
// You can optionally experiment here.
|
// You can optionally experiment here.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user