0
0
Rustprogramming~10 mins

Generic structs in Rust - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AT
BU
CV
DX
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.
2fill in blank
medium

Complete 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'
Af64
Bbool
CString
Di32
Attempts:
3 left
💡 Hint
Common Mistakes
Using a type that does not match the field values.
Omitting the type parameter when creating the instance.
3fill in blank
hard

Fix 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'
AT
BU
CV
DX
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong generic parameter name for the first type.
Not declaring enough generic parameters for the fields.
4fill in blank
hard

Fill 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'
AT
BV
CU
DW
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of generic parameters.
Using generic parameters that don't match the field types.
5fill in blank
hard

Fill 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'
AX
BY
CZ
DT
Attempts:
3 left
💡 Hint
Common Mistakes
Using fewer generic parameters than fields.
Using generic parameters that don't match the field names.