Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new instance of the struct Point with x set to 5.
Rust
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: [1], y: 0 };
println!("({}, {})", p.x, p.y);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the field name instead of a value.
Setting
x to zero instead of 5.✗ Incorrect
To create a struct instance, you assign values to its fields. Here,
x should be 5.2fill in blank
mediumComplete the code to create a Rectangle instance with width 10 and height 20.
Rust
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let rect = Rectangle { width: [1], height: 20 };
println!("{} x {}", rect.width, rect.height);
}Attempts:
3 left
💡 Hint
Common Mistakes
Confusing
width and height values.Using the field name instead of a number.
✗ Incorrect
To create the rectangle, set
width to 10 as required.3fill in blank
hardFix the error in creating a User instance by completing the missing field value.
Rust
struct User {
username: String,
active: bool,
}
fn main() {
let user = User { username: String::from("alice"), active: [1] };
println!("User active: {}", user.active);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around boolean values.
Using capitalized
True which is invalid in Rust.✗ Incorrect
The
active field is a boolean and should be set to true without quotes.4fill in blank
hardFill both blanks to create a Book instance with title "Rust" and pages 300.
Rust
struct Book {
title: String,
pages: u32,
}
fn main() {
let book = Book { title: String::from([1]), pages: [2] };
println!("{} has {} pages", book.title, book.pages);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using numbers as strings or strings without quotes.
Mixing up the number of pages.
✗ Incorrect
The title is a string so it needs quotes, and pages is a number without quotes.
5fill in blank
hardFill all three blanks to create a Car instance with brand "Tesla", year 2022, and electric true.
Rust
struct Car {
brand: String,
year: u32,
electric: bool,
}
fn main() {
let car = Car {
brand: String::from([1]),
year: [2],
electric: [3],
};
println!("{} {} electric: {}", car.brand, car.year, car.electric);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around boolean values.
Using lowercase brand without quotes.
Mixing up the year value.
✗ Incorrect
Brand is a string needing quotes, year is a number, and electric is a boolean true without quotes.
