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

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

别名

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

相关推荐
问道飞鱼4 小时前
【Rust开发知识】Actix-web 开发环境搭建完整教程
开发语言·前端·rust·actix-web
古城小栈5 小时前
PyO3 库全介绍
python·rust
古城小栈5 小时前
Rust 中符号语法 一文全晓
开发语言·后端·rust
古城小栈6 小时前
Rust 模块管理与文件联动
开发语言·后端·rust
古城小栈6 小时前
内存对决:rust、go、java、python、nodejs
java·golang·rust
superman超哥1 天前
Rust 错误处理模式:Result、?运算符与 anyhow 的最佳实践
开发语言·后端·rust·运算符·anyhow·rust 错误处理
Tony Bai2 天前
高并发后端:坚守 Go,还是拥抱 Rust?
开发语言·后端·golang·rust
哆啦code梦2 天前
Rust:高性能安全的现代编程语言
开发语言·rust
superman超哥2 天前
Rust 过程宏开发入门:编译期元编程的深度实践
开发语言·后端·rust·元编程·rust过程宏·编译期
借个火er2 天前
用 Tauri 2.0 + React + Rust 打造跨平台文件工具箱
react.js·rust