别名 Type Alias
使用 type 可用于定义一个类型别名, 类似于 C 中的 typedef 或者 C++ 中的 using TypeA = TypeB;.
使用 use 可将某模块中的内容或某枚举类型中的元素导入到当前作用域, 跟 C++ 中的 using 有些类似.
using GlyphId = int32_t;
同样的类型在 Rust 中可以这样写:
pub type GlyphId = i32;
还可以使用不流行的写法:
pub use i32 as GlyphId;
在导入外部的类型时, 可以使用 use Foo as Bar 的语法给它加上别名:
use color::Color as ArgbColor;
fn main() {
let text_color = ArgbColor::new(255, 39, 88, 0x0a);
println!("color: {text_color:?}");
}
pub mod color {
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Color {
pub alpha: u8,
pub red: u8,
pub green: u8,
pub blue: u8,
}
impl Color {
#[must_use]
#[inline]
pub const fn new(alpha: u8, red: u8, green: u8, blue: u8) -> Self {
Self {
alpha,
red,
green,
blue,
}
}
}
}