Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a generic struct named Point with one type parameter T.
Rust
struct Point<[1]> {
x: T,
y: T,
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to declare the generic type parameter after the struct name.
Using a different letter in the struct declaration and fields.
✗ Incorrect
The generic type parameter is declared inside angle brackets after the struct name. Here,
T is the type parameter used for the fields.2fill in blank
mediumComplete the code to create an instance of Point with i32 type.
Rust
let p = Point::<[1]>{ x: 5, y: 10 };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a type that does not match the field values.
Omitting the type parameter when creating the instance.
✗ Incorrect
To create a generic struct instance with a specific type, use the syntax
Point::. Here, i32 is the integer type.3fill in blank
hardFix the error in the struct definition to allow different types for x and y.
Rust
struct Point<[1], U> {
x: T,
y: U,
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong generic parameter name for the first type.
Not declaring enough generic parameters for the fields.
✗ Incorrect
To have different types for
x and y, declare two generic parameters T and U. The first parameter must be T to match the field x.4fill in blank
hardFill both blanks to define a generic struct Pair with two type parameters and fields of those types.
Rust
struct Pair<[1], [2]> { first: T, second: U, }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of generic parameters.
Using generic parameters that don't match the field types.
✗ Incorrect
The struct has two generic parameters
T and U. The fields first and second use these types respectively.5fill in blank
hardFill all three blanks to create a generic struct Triple with three type parameters and fields.
Rust
struct Triple<[1], [2], [3]> { a: X, b: Y, c: Z, }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using fewer generic parameters than fields.
Using generic parameters that don't match the field names.
✗ Incorrect
The struct has three generic parameters
X, Y, and Z. The fields a, b, and c use these types respectively.