通过例子学 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
类型推断

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

别名

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

相关推荐
FleetingLore20 小时前
Rust | str 常用方法
rust
一只小松许️2 天前
深入理解 Rust 的内存模型:变量、值与指针
java·开发语言·rust
m0_480502643 天前
Rust 登堂 之 Cell 和 RefCell(十二)
开发语言·后端·rust
烈风3 天前
011 Rust数组
开发语言·后端·rust
Dontla4 天前
Turbopack介绍(由Vercel开发的基于Rust的高性能前端构建工具,用于挑战传统构建工具Webpack、vite地位)Next.js推荐构建工具
前端·rust·turbopack
开心不就得了5 天前
构建工具webpack
前端·webpack·rust
ftpeak5 天前
《WebAssembly指南》第九章:WebAssembly 导入全局字符串常量
开发语言·rust·wasm
红烧code6 天前
【Rust GUI开发入门】编写一个本地音乐播放器(15. 记录运行日志)
rust·gui·log·slint
JordanHaidee6 天前
【Rust GUI开发入门】编写一个本地音乐播放器(15. 记录运行日志)
rust
璨sou6 天前
Rust语言--基础入门到应用
后端·rust