Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Псевдонимы типов

Псевдоним типа создаёт имя для другого типа. Оба типа можно использовать взаимозаменяемо.

// Copyright 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0

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.
  • Newtype часто является лучшей альтернативой, так как создаёт отдельный тип. Предпочитайте struct InventoryCount(usize) вместо type InventoryCount = usize.

  • Программисты на C узнают в этом что-то похожее на typedef.