Chuyển đến nội dung chính
eLearner.app
Mô-đun 5 · Bài học 1 trong tổng số 29/14 trong khóa học~12 min
Bài học theo mô-đun (1/2)

Generics và chức năng

Generics allow you to write flexible, reusable code, avoiding duplication of logic for different data types. Instead of defining multiple functions or structures for different types (like i32, f64, or String), we can use a generic type parameter, conventionally represented by the letter T.

The Rust compiler manages generics using a process called monomorphization: during compilation, it generates a copy of the generic code for each concrete type that is actually used. This means there is zero runtime performance overhead.


Generic Functions

To define a generic function, we insert the type parameter <T> right after the function name and before the argument list:

Code
fn print_value<T: std::fmt::Debug>(value: T) {
    println!("Value: {:?}", value);
}

In generic functions, we can use the generic type T for both argument types and return types:

Code
fn identity<T>(value: T) -> T {
    value
}

Generic Structs

We can also use generic type parameters inside structures (struct) to define flexible fields:

Code
struct KeyValuePair<K, V> {
    key: K,
    value: V,
}

fn main() {
    let pair = KeyValuePair {
        key: String::from("age"),
        value: 30,
    };
}

In an impl block for a generic struct, we must declare the type parameter <T> immediately after the impl keyword to indicate we are implementing methods on a generic structure:

Code
struct Container<T> {
    value: T,
}

impl<T> Container<T> {
    fn new(value: T) -> Self {
        Container { value }
    }

    fn value(&self) -> &T {
        &self.value
    }
}

Try it yourself

Exercise 1: The Point Struct

tập thể dục#rust.m5.l1.e1
Nỗ lực: 0Đang tải…

Define a generic struct named Point<T> with two fields: x of type T and y of type T. In the main function, instantiate a variable point containing a Point with integer values x = 5 and y = 10, then print the value of point.x.

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

Declare the struct using `struct Point<T> { x: T, y: T }`. Instantiate it in main and use `point.x` to print it.

Giải pháp khả dụng sau 3 lần thử

Exercise 2: Reversing a Tuple with swap

tập thể dục#rust.m5.l1.e2
Nỗ lực: 0Đang tải…

Write a generic function named swap<T> that accepts a tuple of two elements (T, T) and returns a new tuple (T, T) with their positions reversed. In the main function, call the function with tuple (1, 2) and print the result.

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

The function signature must be `fn swap<T>(pair: (T, T)) -> (T, T)`. Return the reversed tuple with `(pair.1, pair.0)`.

Giải pháp khả dụng sau 3 lần thử

Exercise 3: The Container Struct

tập thể dục#rust.m5.l1.e3
Nỗ lực: 0Đang tải…

Define a generic struct Container<T> containing a value field of type T. Implement a generic impl block to define a new method that accepts a value of type T and returns an instance of Container<T>.

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

Use `impl<T> Container<T>`to implement the associated method`fn new(value: T) -> Self { Container { value } }`.

Giải pháp khả dụng sau 3 lần thử