目录深入类型类型转换newtype和类型别名Sized和不定长类型DST枚举和整数深入类型类型转换从一种类型到另一种类型显式类型转换使用as关键字进行基本类型的显式转换。使用From和Into特征进行更复杂的转换。fnmain(){l
从一种类型到另一种类型
显式类型转换
as
关键字进行基本类型的显式转换。From
和 Into
特征进行更复杂的转换。fn main() {
let a: i32 = 5;
let b: f64 = a as f64; // 显式类型转换
println!("b = {}", b); // 输出: b = 5.0
let c: i64 = 10;
let d: u32 = c as u32; // 显式类型转换
println!("d = {}", d); // 输出: d = 10
let e: i32 = 20;
let f: f64 = From::from(e); // 使用 From 特征
println!("f = {}", f); // 输出: f = 20.0
let g: i32 = 30;
let h: f64 = g.into(); // 使用 Into 特征
println!("h = {}", h); // 输出: h = 30.0
}
定义新类型
newtype
是一种定义新的类型包裹现有类型的方式。struct NewInt(u32);
impl NewInt {
fn new(value: u32) -> Self {
NewInt(value)
}
fn get_value(&self) -> u32 {
self.0
}
}
fn main() {
let ni = NewInt::new(42);
println!("Value: {}", ni.get_value()); // 输出: Value: 42
}
定义类型别名
type AliasInt = i32;
fn main() {
let a: AliasInt = 42;
println!("a = {}", a); // 输出: a = 42
}
Sized 特征
不定长类型(DST)
trait MyTrait {}
impl MyTrait for i32 {}
impl MyTrait for String {}
fn process_data<T: MyTrait + ?Sized>(data: &T) {
println!("Data: {:?}", data);
}
fn main() {
let int_data = 42;
let str_data = String::from("hello");
process_data(&int_data);
process_data(&str_data);
}
枚举
enum MyEnum {
IntVariant(i32),
StrVariant(String),
}
fn main() {
let int_val = MyEnum::IntVariant(42);
let str_val = MyEnum::StrVariant(String::from("hello"));
match int_val {
MyEnum::IntVariant(x) => println!("Integer: {}", x),
MyEnum::StrVariant(s) => println!("String: {}", s),
}
match str_val {
MyEnum::IntVariant(x) => println!("Integer: {}", x),
MyEnum::StrVariant(s) => println!("String: {}", s),
}
}
整数枚举
enum MyIntEnum {
Zero,
One,
Two,
Three,
}
fn main() {
let zero = MyIntEnum::Zero;
let one = MyIntEnum::One;
let two = MyIntEnum::Two;
let three = MyIntEnum::Three;
match zero {
MyIntEnum::Zero => println!("Zero"),
MyIntEnum::One => println!("One"),
MyIntEnum::Two => println!("Two"),
MyIntEnum::Three => println!("Three"),
}
}
模式匹配
enum MyEnum {
IntVariant(i32),
StrVariant(String),
}
fn process_enum(e: MyEnum) {
match e {
MyEnum::IntVariant(x) => println!("Integer: {}", x),
MyEnum::StrVariant(s) => println!("String: {}", s),
}
}
fn main() {
let int_val = MyEnum::IntVariant(42);
let str_val = MyEnum::StrVariant(String::from("hello"));
process_enum(int_val);
process_enum(str_val);
}
枚举和整数的隐式转换
#[derive(Debug)]
enum MyIntEnum {
Zero = 0,
One = 1,
Two = 2,
Three = 3,
}
fn main() {
let zero: MyIntEnum = MyIntEnum::Zero;
let one: MyIntEnum = MyIntEnum::One;
let two: MyIntEnum = MyIntEnum::Two;
let three: MyIntEnum = MyIntEnum::Three;
println!("Zero: {:?}", zero as i32); // 输出: Zero: 0
println!("One: {:?}", one as i32); // 输出: One: 1
println!("Two: {:?}", two as i32); // 输出: Two: 2
println!("Three: {:?}", three as i32); // 输出: Three: 3
}
枚举和整数的显式转换
#[derive(Debug)]
enum MyIntEnum {
Zero = 0,
One = 1,
Two = 2,
Three = 3,
}
impl From<MyIntEnum> for i32 {
fn from(e: MyIntEnum) -> Self {
match e {
MyIntEnum::Zero => 0,
MyIntEnum::One => 1,
MyIntEnum::Two => 2,
MyIntEnum::Three => 3,
}
}
}
fn main() {
let zero: MyIntEnum = MyIntEnum::Zero;
let one: MyIntEnum = MyIntEnum::One;
let two: MyIntEnum = MyIntEnum::Two;
let three: MyIntEnum = MyIntEnum::Three;
println!("Zero: {:?}", i32::from(zero)); // 输出: Zero: 0
println!("One: {:?}", i32::from(one)); // 输出: One: 1
println!("Two: {:?}", i32::from(two)); // 输出: Two: 2
println!("Three: {:?}", i32::from(three)); // 输出: Three: 3
}
枚举和整数的实际应用
enum State {
Idle,
Running,
Paused,
Stopped,
}
impl State {
fn transition(&self) -> State {
match self {
State::Idle => State::Running,
State::Running => State::Paused,
State::Paused => State::Stopped,
State::Stopped => State::Idle,
}
}
}
fn main() {
let mut state = State::Idle;
for _ in 0..5 {
println!("Current state: {:?}", state);
state = state.transition();
}
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!