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

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

别名

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

相关推荐
Java水解18 小时前
Rust嵌入式开发实战——从ARM裸机编程到RTOS应用
后端·rust
Pomelo_刘金19 小时前
Rust:所有权系统
rust
Ranger09291 天前
鸿蒙开发新范式:Gpui
rust·harmonyos
DongLi014 天前
rustlings 学习笔记 -- exercises/05_vecs
rust
番茄灭世神5 天前
Rust学习笔记第2篇
rust·编程语言
shimly1234565 天前
(done) 速通 rustlings(20) 错误处理1 --- 不涉及Traits
rust
shimly1234565 天前
(done) 速通 rustlings(19) Option
rust
@atweiwei5 天前
rust所有权机制详解
开发语言·数据结构·后端·rust·内存·所有权
shimly1234565 天前
(done) 速通 rustlings(24) 错误处理2 --- 涉及Traits
rust
shimly1234565 天前
(done) 速通 rustlings(23) 特性 Traits
rust