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

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

别名

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

相关推荐
heroboyluck1 天前
rust 实例化动态对象
开发语言·rust·trait
Source.Liu1 天前
【CXX】4 跨平台构建系统特性对比
c++·rust·cxx
Hello.Reader1 天前
深入探讨 Rust 中的 Deref Trait:让智能指针像常规引用一样工作
开发语言·后端·rust
yoona10201 天前
Rust编程语言入门教程 (七)函数与控制流
开发语言·rust·区块链·学习方法
JD技术委员会1 天前
Rust 未来会成为主流的编程语言吗?
开发语言·后端·rust
无名之逆1 天前
探索 Hyperlane:高性能 Rust Web 框架的崛起
java·开发语言·后端·python·算法·面试·rust
懒羊羊我小弟2 天前
Webpack 基础入门
前端·webpack·rust·node.js·es6
武侠编程3 天前
Rust兵器谱|流星镖:tokio
rust
大雄野比3 天前
rust学习三、基本类型
开发语言·学习·rust