Ana içeriğe geç
eLearner.app
Modül 5 · Ders 1 ders 2Kurstaki 9/14~12 min
Modül dersleri (1/2)

Jenerikler ve Fonksiyonlar

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

Egzersiz#rust.m5.l1.e1
Denemeler: 0Yükleniyor…

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.

Düzenleyici yükleniyor…
İpucu göster

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

3 denemeden sonra çözüm mevcut

Exercise 2: Reversing a Tuple with swap

Egzersiz#rust.m5.l1.e2
Denemeler: 0Yükleniyor…

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.

Düzenleyici yükleniyor…
İpucu göster

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

3 denemeden sonra çözüm mevcut

Exercise 3: The Container Struct

Egzersiz#rust.m5.l1.e3
Denemeler: 0Yükleniyor…

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

Düzenleyici yükleniyor…
İpucu göster

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

3 denemeden sonra çözüm mevcut