مرکزی مواد پر جائیں
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 کوششوں کے بعد حل دستیاب ہے۔