通过例子学 rust 个人精简版 5-all

5

类型转换
rust 复制代码
fn main() {
    let decimal = 65.4321_f32;
    let integer = decimal as u8;
    let character = integer as char;
    println!("Casting: {} -> {} -> {}", decimal, integer, character);
    //Casting: 65.4321 -> 65 -> A
}
bash 复制代码
要点1 : 精度丢失
bash 复制代码
个人 : 不是很能理解 #![allow(overflowing_literals)] 的用途 所以这里我删掉了
字面量
rust 复制代码
fn main() {
    // 带后缀的字面量,其类型在初始化时已经知道了。
    let x = 1u8;
    let y = 2u32;
    let z = 3f32;

    // 无后缀的字面量,其类型取决于如何使用它们。
    let i = 1;
    let f = 1.0;

    // `size_of_val` 返回一个变量所占的字节数
    println!("size of `x` in bytes: {}", std::mem::size_of_val(&x));
    println!("size of `y` in bytes: {}", std::mem::size_of_val(&y));
    println!("size of `z` in bytes: {}", std::mem::size_of_val(&z));
    println!("size of `i` in bytes: {}", std::mem::size_of_val(&i));
    println!("size of `f` in bytes: {}", std::mem::size_of_val(&f));
}
bash 复制代码
size of `x` in bytes: 1
size of `y` in bytes: 4
size of `z` in bytes: 4
size of `i` in bytes: 4
size of `f` in bytes: 8
类型推断

之前有提到过,没啥好说的

别名

之前有提到过,没啥好说的

相关推荐
迷渡6 小时前
用 Rust 重写的 Bun 有 13365 个 unsafe!
开发语言·后端·rust
咸甜适中7 小时前
rust语言学习笔记Trait(九)PartialEq、 Eq(相等比较)
笔记·学习·rust
恋喵大鲤鱼9 小时前
Rust 中 package crate 和 module 的关系
rust
Rust研习社10 小时前
Rust 官方拟定 LLM 政策,防止 LLM 污染开源社区?
开发语言·后端·ai·rust·开源
techdashen10 小时前
Async Rust 近况补课:从 `async-trait` 到原生 async trait
网络·算法·rust
techdashen11 小时前
在 Async Rust 中实现请求合并(Request Coalescing)
开发语言·后端·rust
咸甜适中1 天前
rust语言学习笔记Trait(八)Iterator(迭代器)
笔记·学习·rust
zoomdong1 天前
@utoo/pack: 基于 Turbopack 的下一代 Rust 构建工具
webpack·rust·开源
数据法师2 天前
MotrixNext:接棒经典 Motrix,用 Tauri 2+Rust 重构的下一代开源下载神器
重构·rust·开源