githubopen in new window

hello rust

basic

1.3variables

TIP

变量默认是不可变的,使用mut使其可变

1.4functions

1.5println!

1.6match

loop

while

enum 枚举

struct 结构体

struct enum 应用

number calculate

match default

guess game (可恶的是,https://play.rust-lang.org/ playground不能使用控制台)

输入你猜测的数字

显示“秘密数字”

Ordering是两值之间比较的结果

guess game最终版

rust语言圣经练习open in new window

1.1安装环境

rustup self uninstall
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
ajn404@ajn404deMacBook-Air sit-pad % cargo -V
cargo 1.65.0 (4bc8f24d3 2022-10-20)
ajn404@ajn404deMacBook-Air sit-pad % rustc -V
rustc 1.65.0 (897e37553 2022-11-02)
cargo new hello_world
cd hello_world
cargo run 
cargo build
cargo check
cargo run --release
cargo build --release

cargo.toml是cargo 特有的项目描述文件,cargo.lock是项目依赖详细清单

TIP

.lock在可运行项目中需上传git,在包/库项目中写在gitignore中

1.4 hello world

1.6避免从入门到放弃

链表在 Rust 中简直是地狱一般的难度,我见过太多英雄好汉难过链表关,最终黯然退幕

2.1绑定

习题open in new window答案open in new window

1.绑定与可变性

3.变量作用域

4.🌟🌟

5.shadowing

6.🌟🌟删除一行

7.使用以下方法来修复编译器输出的 warning : 🌟 一种方法 🌟🌟 两种方法

8.变量解构

my own solution 变量遮蔽

可变性

9,解构式赋值

my own solution

2.2基本类型

2.2.1 数值类型

整型

i8,u8...

TIP

rust整数类型默认使用i32
panic:表示程序崩溃退出
整型溢出,补码循环溢出

浮点类型

精度控制

NaN

is_nan

/*copy

运算

综合示例

位运算

运算符说明
& 位与相同位置均为1时则为1,否则为0
| 位或相同位置只要有1时则为1,否则为0
^ 异或相同位置不相同则1,相同则为0
! 位非把位中的0和1相互取反,即0置为1,1置为0
<< 左移所有位向左移动指定位数,右位补0
>> 右移所有位向右移动指定位数,带符号移动(正数补0,负数补1

序列

TIP

1..5不包函5
1..=5包函5

有理数和复数

习题open in new window 答案open in new window

1.整数

my own solution

2.✨

3.自动推导类型

4.i有符号 u无符号

  1. overflow

7.浮点数

9.range🌟🌟 两个目标: 1. 修改 assert! 让它工作 2. 让 println! 输出: 97 - 122

10

.....效率太低了,等我学完再整理

copy demo

net about

cnopen in new window
exampleopen in new window
bookopen in new window
apiopen in new window
crates(rust lib)open in new window

实践项目

tauriopen in new window

申明

本页面的编译结果来源为rust playgroundopen in new window,感谢开源,thanks ©MIT

本页面全是组件,性能变差,组件写得不好

crates

UTF8 Sliceopen in new window

  • A lightweight heapless way to do slicing on unicode strings in Rust.
use utf8_slice;

fn main() {
    let s = "holla中国人नमस्ते";
    // for item in s.bytes(){
    //     println!("{}",item);
    // }
    
    for item in s.chars() {
        println!("{}", item);
    }

    let sub_s = utf8_slice::slice(s, 2, s.len() - 2);
    println!("{}", sub_s);
}