Rust

Associated types

Në shqip: Tipet e shoqëruara

ShpjegimiSQ

Një trait mund të ketë një tip „të shoqëruar“ që e vendos çdo implementim — p.sh. Iterator ka type Item, që tregon çfarë lloj elementesh jep. Kështu s'ke pse e shkruan si gjenerik kudo.

EnglishEN

A trait can have an “associated” type that each implementation sets — e.g. Iterator has type Item, saying what kind of items it yields. That way you don't write it as a generic everywhere.

Si ta mendosh

Si makina e kafesë: çdo model vendos vetë çfarë pije nxjerr.

Lexoje në anglisht

Like a coffee machine: each model decides what drink it makes.

Shembull kodirust

impl Iterator for Numeruesi {
    type Item = u32;
    fn next(&mut self) -> Option<u32> { self.n += 1; Some(self.n) }
}

Terma të lidhur