型エイリアス

型エイリアスは、別の型に対する名前を作成します。この 2 つの型は互いに置き換えて使用できます。

// 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>>>>;
  • newtype は、別個の型を作成するため、よりよい代替手段であることがよくあります。 type InventoryCount = usize ではなく struct InventoryCount(usize) を優先してください。

  • C プログラマーには、これは typedef に似たものだとわかるでしょう。