Newtype pattern
Në shqip: Modeli newtype
ShpjegimiSQ
Një „newtype“ është një struct me një fushë të vetme që mbështjell një tip tjetër: struct Metra(f64);. Kështu kompajleri s'të lejon t'i ngatërrosh metrat me kilometrat, edhe pse të dyja janë f64.
EnglishEN
A “newtype” is a struct with a single field wrapping another type: struct Metres(f64);. That way the compiler won't let you mix up metres and kilometres, even though both are f64.
Si ta mendosh
Si dy shishe me të njëjtin lëng por me etiketa të ndryshme.
Lexoje në anglisht
Like two bottles of the same juice with different labels.
Shembull kodirust
struct Metra(f64);
struct Kilometra(f64);
fn vrapo(d: Kilometra) {}
// vrapo(Metra(500.0)); // Gabim