generic

Rust Methods for one concrete generic type

A method can belong only to a particular instantiation. A text label needs text-specific operations that a numeric label does not need. Expected output (not executed): 4 Why: Only Label<String> has letters from this impl. Common mistake: Confusing a concrete inherent impl with unstable trait specialization.

Formal Syntax & Structure

rust syntaxrust
impl Label<String> { /* methods */ }

Working Example

Example Snippetrust
struct Label<T>(T);impl Label<String> {    fn letters(&self) -> usize { self.0.chars().count() }}fn main() {    let text = Label(String::from("Desk"));    println!("{}", text.letters());}
Rust Reference