跳转到主要内容
eLearner.app
模块 5 · 第 1 课(共 2)课程中的9/14~12 min
模块课程(1/2)

泛型和函数

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

锻炼#rust.m5.l1.e1
尝试:0加载中...

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.

正在加载编辑器...
显示提示

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

3 次尝试后可用的解决方案

Exercise 2: Reversing a Tuple with swap

锻炼#rust.m5.l1.e2
尝试:0加载中...

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.

正在加载编辑器...
显示提示

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

3 次尝试后可用的解决方案

Exercise 3: The Container Struct

锻炼#rust.m5.l1.e3
尝试:0加载中...

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>.

正在加载编辑器...
显示提示

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

3 次尝试后可用的解决方案