Псевдоніми типу
Псевдонім типу створює ім’я для іншого типу. Ці два типи можна використовувати взаємозамінно.
enum CarryableConcreteItem {
Left,
Right,
}
type Item = CarryableConcreteItem;
// Псевдоніми більш корисні для довгих, складних типів:
use std::cell::RefCell;
use std::sync::{Arc, RwLock};
type PlayerInventory = RwLock<Vec<Arc<RefCell<Item>>>>;
This slide should take about 2 minutes.
-
A newtype is often a better alternative since it creates a distinct type. Prefer
struct InventoryCount(usize)totype InventoryCount = usize. -
Програмісти на C впізнають це як схоже на
typedef.