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

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

别名

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

相关推荐
白应穷奇13 分钟前
Diesel的类型安全: 深入理解Rust ORM的类型系统
rust
Hello.Reader37 分钟前
Rust + WebAssembly 上线实战指南
开发语言·rust·wasm
许野平38 分钟前
Rust:如何开发Windows 动态链接库 DLL
windows·单片机·rust·dll·动态链接库
Rust语言中文社区39 分钟前
Rust 训练营二期来袭: Rust + AI 智能硬件
开发语言·后端·rust
Jacob023421 小时前
Node.js 性能瓶颈与 Rust + WebAssembly 实战探索
后端·rust·node.js
寻月隐君1 天前
Rust 核心设计:孤儿规则与代码一致性解析
后端·rust·github
许野平2 天前
Rust 同步方式访问 REST API 的完整指南
java·网络·rust·restful
许野平2 天前
Rust:如何访问 *.ini 配置文件?
开发语言·数据库·rust·ini·configparser
许野平2 天前
Rust:开发 DLL 动态链接库时如何处理 C 字符串
c语言·开发语言·rust·字符串·动态库·dll
许野平2 天前
Rust: 获取 MAC 地址方法大全
开发语言·macos·rust·mac