惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家
H
Help Net Security
腾讯CDC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The GitHub Blog
The GitHub Blog
V
V2EX
M
MIT News - Artificial intelligence
Vercel News
Vercel News
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
阮一峰的网络日志
阮一峰的网络日志
B
Blog RSS Feed
D
Docker
V
Visual Studio Blog
博客园 - 叶小钗
美团技术团队
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
[Rust Guide] 10.2. Generics
SomeB1oody · 2026-04-27 · via DEV Community

If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.

10.2.1 What Are Generics

The main purpose of generics is to improve code reusability. They are suitable for handling repeated-code problems, and can also be seen as separating data from algorithms.

Generics are abstract substitutes for concrete types or other attributes. In other words, generic code is not the final code you write; it is more like a template with some placeholders.

The compiler replaces those placeholders with concrete types at compile time. Let’s look at an example:

fn largest<T>(list:&[T]) -> T {
//......
}

Enter fullscreen mode Exit fullscreen mode

This function definition uses a generic type parameter. T is the so-called “placeholder.” When you write the code, T can represent any type, but during compilation the compiler replaces T with a concrete type based on the actual usage. This process is called monomorphization.

T is the generic type parameter. In fact, you can use any valid identifier as the type-parameter name, but by convention people usually use an uppercase T (for Type). When choosing a generic type-parameter name, it is usually very short; one letter is often enough. If you really want to make it longer, use camel-case naming.

10.2.2 Generics in Function Definitions

When defining a function with generics, you need to place the generic type parameter in the function signature. Generic type parameters are usually used to specify parameter and return types.

Using the code from the previous article as an example, here it is with a small generic modification:

fn largest<T>(list: &[T]) -> T{  
    let mut largest = list[0];  
    for &item in list{  
        if item > largest{  
            largest = item;  
        }  
    }  
    largest  
}

Enter fullscreen mode Exit fullscreen mode

You can understand the whole function definition like this: the function largest has a generic type parameter T, it accepts a slice as its argument, the slice’s elements are of type T, and the return value is also of type T.

Try compiling it, and the output is:

error[E0369]: binary operation `>` cannot be applied to type `T`
 --> src/main.rs:4:17
  |
4 |         if item > largest{
  |            ---- ^ ------- T
  |            |
  |            T
  |
help: consider restricting type parameter `T`
  |
1 | fn largest<T: std::cmp::PartialOrd>(list: &[T]) -> T{
  |             ++++++++++++++++++++++

Enter fullscreen mode Exit fullscreen mode

For now, we won’t discuss the reason or how to fix it. You only need to know that this is roughly how generic parameters are written. Later articles will explain how to specify a particular trait.

10.2.3 Generics in struct Definitions

Generic type parameters defined in structs are mainly used in their fields. For example:

struct Point<T> {   
    x: T,  
    y: T,  
}  

fn main() {  
    let integer = Point { x: 5, y: 10 };  
    let float = Point { x: 1.0, y: 4.0 };  
}

Enter fullscreen mode Exit fullscreen mode

Add <> after the struct name and write the generic parameter name inside it, and that generic type can be applied to each field in the struct.

In main, this struct is instantiated. The two fields in integer are both i32, and the two fields in float are both f64. Because x and y are both declared as T, the instantiated x and y must also be the same type. The two types must remain consistent.

What if I want x and y to be two different types? Easy: declare two generic type parameters.

struct Point<T, U> {   
    x: T,  
    y: U,  
}  

fn main() {  
    let integer = Point { x: 5, y: 1.0 };  
    let float = Point { x: 1.0, y: 40 };  
}

Enter fullscreen mode Exit fullscreen mode

At this point, the instantiated x and y can be different types, of course they can also be the same type.

Note that although multiple generic type parameters are allowed, too many generics will reduce readability. Usually, that means the code should be reorganized into more, smaller units.

10.2.4 Generics in enum Definitions

Much like structs, generic type parameters in enums are mainly used in their variants, allowing enum variants to hold generic data types. The most common examples are Option<T> and Result<T, E>.

For example:

enum Option<T> {  
    Some(T),  
    None,  
}  

enum Result<T, E> {  
    Ok(T),  
    Err(E),  
}

Enter fullscreen mode Exit fullscreen mode

  • In the Option enum, Some(T) is the variant that holds a value of type T, while the None variant means it holds no value. Because the Option enum uses generics, Option<T> can represent a possible value no matter what type that value is
  • Likewise, an enum can use multiple generic type parameters. For example, the Result enum uses T and E: the Ok variant stores T, and the Err variant stores E

10.2.5 Generics in Method Definitions

Methods can be attached to enums or structs. Since enums and structs can use generic parameters, methods can too, as shown here:

struct Point<T> {   
    x: T,  
    y: T,  
}  

impl<T> Point<T> {  
    fn x(&self) -> &T {  
        &self.x  
    }  
}

Enter fullscreen mode Exit fullscreen mode

The x method is essentially a getter. When implementing methods for Point<T>, you need to add <T> after the impl keyword. This indicates that the implementation is for generic T, not for some concrete type.

Of course, if you are implementing a method for a specific type, you do not need that:

impl Point<i32> {  
    fn x1(&self) -> &i32 {  
        &self.x  
    }  
}

Enter fullscreen mode Exit fullscreen mode

The x1 method exists only on the concrete type Point<i32>, and other Point<T> types do not have this method, similar to specialization and partial specialization in C++.

Another important point is that the generic type parameters in the struct can differ from the generic type parameters in the method. For example:

struct Point<T, U> {   
    x: T,  
    y: U,  
}  

impl<T, U> Point<T, U> {  
    fn mixup<V, W>(self, other: Point<V, W>) -> Point<T, W> {  
        Point {  
            x: self.x,  
            y: other.y,  
        }  
    }  
}  

fn main() {  
    let p1 = Point { x: 5, y: 10.4 };  
    let p2 = Point { x: "Hello", y: 'c' };  
    let p3 = p1.mixup(p2);  
    println!("p3.x = {}, p3.y = {}", p3.x, p3.y);  
}

Enter fullscreen mode Exit fullscreen mode

The method mixup is implemented for Point<T, U>. It has two generic type parameters, V and W. The two type parameters in the method are different from the two type parameters in Point, although the actual types may also end up being the same. The second parameter of mixup is other, whose type is also Point, but that Point does not necessarily use the same data types as the Point referred to by self, so two new generic type parameters are needed. Looking at the return type, it is Point<T, W>: T comes from Point<T, U>, and W comes from Point<V, W>.

Now look at main: first p1 is declared, and both of its fields are i32; then p2 is declared, and its two fields are &str (string slice) and char (a single character, represented with ''). Then mixup is used. p1 corresponds to Point<T, U>, and p2 corresponds to Point<V, W>. From their field types, we can infer that T is i32, U is i32, V is &str, and W is char. The return type of mixup is Point<T, W>, which in this example becomes Point<i32, char>.

Output:

p3.x = 5, p3.y = c

Enter fullscreen mode Exit fullscreen mode

10.2.6 Performance of Generic Code

Code written with generics runs just as fast as code written with concrete types. Rust performs monomorphization at compile time, replacing generic types with concrete types, so there is no type-substitution process during execution.

For example:

fn main() {
    let integer = Some(5);
    let float = Some(5.0);
}

Enter fullscreen mode Exit fullscreen mode

Here integer is Option<i32>, and float is Option<f64>. During compilation, the compiler expands Option<T> into Option_i32 and Option_f64:

enum Option_i32 {
    Some(i32),
    None,
}

enum Option_f64 {
    Some(f64),
    None,
}

Enter fullscreen mode Exit fullscreen mode

In other words, the generic definition Option<T> is replaced by two concrete type definitions.

The monomorphized main function also becomes this:

enum Option_i32 {
    Some(i32),
    None,
}

enum Option_f64 {
    Some(f64),
    None,
}

fn main(){
    let integer = Option_i32::Some(5);
    let float = Option_f64::Some(5.0);
}

Enter fullscreen mode Exit fullscreen mode