Rust是一门赋予每个人构建可靠且高效软件能力的编程语言。可靠主要体现在安全性上。其高效不仅限于开发效率,它的执行效率也是令人称赞的,是一种少有的兼顾开发效率和执行效率的语言。Rust 语言由 Mozilla 开发,最早发布于 2014 年 9 月。Rust 的编译器是在 MIT License 和 Apache License 2.0 双重协议声明下的免费开源软件。
高性能:Rust 速度惊人且内存利用率极高。由于没有运行时和垃圾回收,它能够胜任对性能要求特别高的服务,可以在嵌入式设备上运行,还能轻松和其他语言集成。
可靠性:Rust 丰富的类型系统和所有权模型保证了内存安全和线程安全,让您在编译期就能够消除各种各样的错误。
生产力:Rust 拥有出色的文档、友好的编译器和清晰的错误提示信息, 还集成了一流的工具——包管理器和构建工具, 智能地自动补全和类型检验的多编辑器支持, 以及自动格式化代码等等。
Rustacean:使用 rust 的攻城狮不叫 ruster 而是叫 Rustacean ,咱也不知道为什么,书上就是这么说的。
以 windows 11 为例
下载 rustup-init.exe ,双击此可执行程序会打开一个命令行程序,此程序引导安装,具体安装过程:
示例:
输出:
整数,浮点,布尔,字符
可以将多个值放到一个数据类型中。
和大多数编程语言一样, if - else if - else 表示条件语句,在 if 后面不用加括号。
花括号中套花括号就可以输出花括号
示例代码:
以上代码输出:
所有权可以理解为命名空间+作用域+指针。
特殊的数据类型--->类型的引用(即指针),使用 & 关键字表示
可变变量也可以有指针
示例代码:
以上代码输出:
切片是指向数据结构(字符串、集合)一部分内容的引用。
不愿意将rust中的切片理解为一种“类型”,实际上也不是;更不愿将rust中的切片理解为一种“集合”。暂且将切片理解成一种对象吧。例如 &s[0..5] 就是获取到了字符串 s 索引从0到5位置的元素,包含0不包含5。
rust中的切片部分主要是要理解索引和下标的概念。
示例代码:
以上代码输出:
struct 类似 java 中的类,用来自定义一种数据结构,这种数据结构一般用来描述生活中的某一个对象。struct 中可以包含属性和方法。使用结构体分为两步:首先需要定义一个结构体,然后需要实例化一个结构体,再然后才可以使用。
一旦结构体实例化的时候是可变的,即使用 mut 修饰, 那么结构体中所有的属性都将是可变的。
结构体属性:
示例代码:
以上代码输出:
枚举表示某一个对象可能的值,实际使用中常用来表达:ip地址类型,订单状态,人员性别,实名认证方式等某一事物简短又不经常变化的“可选值”。
枚举成员:
Option 是一个标准库中的枚举,用来处理空值(null) 的情况。Option是一个泛型枚举,接受类型 T 。Option 要干的事情和java 中的 optional(java8新特性) 类似。
简言之:
Option 源码:
使用举例:
以上代码输出:
Rust 标准库中包含一系列被称为 集合(collections)的非常有用的数据结构。大部分其他数据类型都代表一个特定的值,不过集合可以包含多个值。不同于内建的数组和元组类型,集合指向的数据是储存在堆上的,这意味着数据的数量不必在编译时就已知,并且还可以随着程序的运行增长或缩小。
vector 允许我们在一个单独的数据结构中储存多个值,所有值在内存中彼此相邻排列。vector 只能储存相同类型的值。
如果借助枚举,有时候 vector 也可以变相存储不同类型的值。
示例代码:
以上代码输出:
你没有看错,rust 中,字符串也是集合,是什么集合呢? 是“字符”的集合。注意:上面提到过字符是 rust 的基础标量类型,但是字符串不是标量类型,而是集合类型。
更新字符串的方式:
示例代码:
以上代码输出:
讲道理,rust 中的代码组织相比 java 、CSharp 这些所谓高级语言,要复杂的多。大概包含两个部分:命名空间和访问权限。
rust 中针对包管理主要有三个概念:包(package),箱(crate),模块(module)。三个概念形成一个树状结构,包中包含箱,箱中包含模块。
Rust 中默认所有项(函数、方法、结构体、枚举、模块和常量)都是私有的。父模块中的项不能使用子模块中的私有项,但是子模块中的项可以使用他们父模块中的项。
关键字:
如果 other_rs_file.rs 中定义了子 module , main.rs 仅需导入顶层 module (即 mod other_rs_file) ,使用 mod::子mod::对象名 访问其中对象
rust 默认将rs文件识别为一个 module 处理, 但是无法将文件夹识别为一个 module ,所以如果我们要访问entity文件夹中dept.rs文件中的对象,需要在entiry文件夹中新增一个mod.rs 并在其中定义 dept module ,访问其中对象的时候同样仅需导入顶层 mod ,然后通过 entity::dept::Dept 方式访问 dept 对象。
工程目录文件结构:
示例代码:
other_rs_file.rs:
dept.rs:
mod.rs:
main.rs:
main输出:
示例代码:
Cargo.toml:
main.rs:
工程结构:
示例代码:
以上代码输出:
泛型这种编程语言得设计绝非 rust 独有的,实际上在C# , Java 这些语言中早就引入了泛型的设计思想。泛型即类型的泛化,在编写代码的时候并不知道具体的数据类型或者数据结构是什么样子的,而是定义一个标识,在运行时此标识可动态替换为实际的数据类型。很显然这种设计能够让开发人员避免编写很多相似的重复的代码。
在 rust 中,可以在如下位置编写泛型代码:
示例代码:
以上代码输出:
特性(trait)概念接近于 Java 中的接口(Interface),但两者不完全相同。特性与接口相同的地方在于它们都是一种行为规范,可以用于标识哪些类有哪些方法
一个对象的行为由其可供调用的方法构成。如果可以对不同类型调用相同的方法的话,这些类型就可以共享相同的行为了。trait 定义是一种将方法签名组合起来的方法,目的是定义一个实现某些目的所必需的行为的集合。
示例代码:
以上代码输出:
rust 标准库中的 std::fs 可以用来操作文件
示例代码:
以上代码输出:
面向对象编程(OOP)思想是一个概念,是一种思想指导,围绕“对象”展开,从万物皆是对象的角度出发,一个 crate , 一个 module ,一个 struct ,一个枚举等等都是一个个独立的对象,能自主表达一个事物、某种特征。而运用封装、继承、多态等手段可以让对象与对象之间产生某种联系,进而表达更多的事物,解决更多的问题。
面向对象思想是构建大型应用软件系统的基石。
rust 语言中 可以通过 结构体,枚举,特性(trait)等来实现oop思想。
rust 中的并发编程主要得益与 线程 、 消息传递和互斥锁。
Rust 语言是满足多线程特性的,所以 rust 可以满足 主-->子 多任务应用场景。
消息传递有点类似与消息队列,但消息队列一般跨进程或线程,而 rust 中的消息传递主要是主子线程中数据的传递。
示例代码:
以上代码输出:
以下示例演示了子线程获得了主线程的发送者 tx,并调用了它的 send 方法发送数据,然后主线程就通过对应的接收者 rx 接收到了发送的数据。
无法在子线程中发送而在另外一个子线程中接收
示例代码:
以上代码输出:
互斥锁(mutex)是 mutual exclusion 的缩写,也就是说,任意时刻,其只允许一个线程访问某些数据。为了访问互斥器中的数据,线程首先需要通过获取互斥器的 锁(lock)来表明其希望访问数据。锁是一个作为互斥器一部分的数据结构,它记录谁有数据的排他访问权。因此,我们描述互斥器为通过锁系统 保护(guarding)其数据。
以下示例演示了在主线程中创建一个值,并在多个子线程中修改此值,最终等所有子线程处理完毕,主线程打印最终值。为了让变量能够跨线程之间共享,引入了std::sync::Arc 这个结构体。
示例代码:
以上代码输出:
本文示例代码维护在gitee上面:https://gitee.com/naylor_personal/rust-hello-world
打开代码仓库中的 Sport 文件夹,有惊喜!!!
Rust Visual C
+
+
prerequisites
Rust requires the Microsoft C
+
+
build tools
for
Visual Studio
2013
or
later, but they don't seem to be installed.
The easiest way to acquire the build tools
is
by installing Microsoft
Visual C
+
+
Build Tools
2019
which provides just the Visual C
+
+
build
tools:
https:
/
/
visualstudio.microsoft.com
/
visual
-
cpp
-
build
-
tools
/
Please ensure the Windows
10
SDK
and
the English language pack components
are included when installing the Visual C
+
+
Build Tools.
Alternately, you can install Visual Studio
2019
, Visual Studio
2017
,
Visual Studio
2015
,
or
Visual Studio
2013
and
during install select
the
"C++ tools"
:
https:
/
/
visualstudio.microsoft.com
/
downloads
/
Install the C
+
+
build tools before proceeding.
If you will be targeting the GNU ABI
or
otherwise know what you are
doing then it
is
fine to
continue
installation without the build
tools, but otherwise, install the C
+
+
build tools before proceeding.
Continue? (y
/
N) y
Welcome to Rust!
This will download
and
install the official compiler
for
the Rust
programming language,
and
its package manager, Cargo.
Rustup metadata
and
toolchains will be installed into the Rustup
home directory, located at:
C:\Users\cml\.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory located at:
C:\Users\cml\.cargo
This can be modified with the CARGO_HOME environment variable.
The cargo, rustc, rustup
and
other commands will be added to
Cargo's
bin
directory, located at:
C:\Users\cml\.cargo\
bin
This path will then be added to your PATH environment variable by
modifying the HKEY_CURRENT_USER
/
Environment
/
PATH registry key.
You can uninstall at
any
time with rustup
self
uninstall
and
these changes will be reverted.
Current installation options:
default host triple: x86_64
-
pc
-
windows
-
msvc
default toolchain: stable (default)
profile: default
modify PATH variable: yes
1
) Proceed with installation (default)
2
) Customize installation
3
) Cancel installation
>
info: profile
set
to
'default'
info: default host triple
is
x86_64
-
pc
-
windows
-
msvc
info: syncing channel updates
for
'stable-x86_64-pc-windows-msvc'
info: latest update on
2022
-
01
-
20
, rust version
1.58
.
1
(db9d1b20b
2022
-
01
-
20
)
info: downloading component
'cargo'
3.8
MiB
/
3.8
MiB (
100
%
)
1.7
MiB
/
s
in
2s
ETA:
0s
info: downloading component
'clippy'
1.6
MiB
/
1.6
MiB (
100
%
)
1.5
MiB
/
s
in
1s
ETA:
0s
info: downloading component
'rust-docs'
18.8
MiB
/
18.8
MiB (
100
%
)
3.3
MiB
/
s
in
5s
ETA:
0s
info: downloading component
'rust-std'
22.9
MiB
/
22.9
MiB (
100
%
)
3.2
MiB
/
s
in
7s
ETA:
0s
info: downloading component
'rustc'
65.2
MiB
/
65.2
MiB (
100
%
)
493.2
KiB
/
s
in
1m
14s
ETA:
0s
info: downloading component
'rustfmt'
2.2
MiB
/
2.2
MiB (
100
%
)
631.2
KiB
/
s
in
3s
ETA:
0s
info: installing component
'cargo'
info: installing component
'clippy'
info: installing component
'rust-docs'
18.8
MiB
/
18.8
MiB (
100
%
)
1.9
MiB
/
s
in
6s
ETA:
0s
info: installing component
'rust-std'
22.9
MiB
/
22.9
MiB (
100
%
)
10.5
MiB
/
s
in
2s
ETA:
0s
info: installing component
'rustc'
65.2
MiB
/
65.2
MiB (
100
%
)
12.2
MiB
/
s
in
5s
ETA:
0s
info: installing component
'rustfmt'
info: default toolchain
set
to
'stable-x86_64-pc-windows-msvc'
stable
-
x86_64
-
pc
-
windows
-
msvc installed
-
rustc
1.58
.
1
(db9d1b20b
2022
-
01
-
20
)
Rust
is
installed now. Great!
To get started you may need to restart your current shell.
This would
reload
its PATH environment variable to include
Cargo's
bin
directory (
%
USERPROFILE
%
\.cargo\
bin
).
Press the Enter key to
continue
.
Rust Visual C
+
+
prerequisites
Rust requires the Microsoft C
+
+
build tools
for
Visual Studio
2013
or
later, but they don't seem to be installed.
The easiest way to acquire the build tools
is
by installing Microsoft
Visual C
+
+
Build Tools
2019
which provides just the Visual C
+
+
build
tools:
https:
/
/
visualstudio.microsoft.com
/
visual
-
cpp
-
build
-
tools
/
Please ensure the Windows
10
SDK
and
the English language pack components
are included when installing the Visual C
+
+
Build Tools.
Alternately, you can install Visual Studio
2019
, Visual Studio
2017
,
Visual Studio
2015
,
or
Visual Studio
2013
and
during install select
the
"C++ tools"
:
https:
/
/
visualstudio.microsoft.com
/
downloads
/
Install the C
+
+
build tools before proceeding.
If you will be targeting the GNU ABI
or
otherwise know what you are
doing then it
is
fine to
continue
installation without the build
tools, but otherwise, install the C
+
+
build tools before proceeding.
Continue? (y
/
N) y
Welcome to Rust!
This will download
and
install the official compiler
for
the Rust
programming language,
and
its package manager, Cargo.
Rustup metadata
and
toolchains will be installed into the Rustup
home directory, located at:
C:\Users\cml\.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory located at:
C:\Users\cml\.cargo
This can be modified with the CARGO_HOME environment variable.
The cargo, rustc, rustup
and
other commands will be added to
Cargo's
bin
directory, located at:
C:\Users\cml\.cargo\
bin
This path will then be added to your PATH environment variable by
modifying the HKEY_CURRENT_USER
/
Environment
/
PATH registry key.
You can uninstall at
any
time with rustup
self
uninstall
and
these changes will be reverted.
Current installation options:
default host triple: x86_64
-
pc
-
windows
-
msvc
default toolchain: stable (default)
profile: default
modify PATH variable: yes
1
) Proceed with installation (default)
2
) Customize installation
3
) Cancel installation
>
info: profile
set
to
'default'
info: default host triple
is
x86_64
-
pc
-
windows
-
msvc
info: syncing channel updates
for
'stable-x86_64-pc-windows-msvc'
info: latest update on
2022
-
01
-
20
, rust version
1.58
.
1
(db9d1b20b
2022
-
01
-
20
)
info: downloading component
'cargo'
3.8
MiB
/
3.8
MiB (
100
%
)
1.7
MiB
/
s
in
2s
ETA:
0s
info: downloading component
'clippy'
1.6
MiB
/
1.6
MiB (
100
%
)
1.5
MiB
/
s
in
1s
ETA:
0s
info: downloading component
'rust-docs'
18.8
MiB
/
18.8
MiB (
100
%
)
3.3
MiB
/
s
in
5s
ETA:
0s
info: downloading component
'rust-std'
22.9
MiB
/
22.9
MiB (
100
%
)
3.2
MiB
/
s
in
7s
ETA:
0s
info: downloading component
'rustc'
65.2
MiB
/
65.2
MiB (
100
%
)
493.2
KiB
/
s
in
1m
14s
ETA:
0s
info: downloading component
'rustfmt'
2.2
MiB
/
2.2
MiB (
100
%
)
631.2
KiB
/
s
in
3s
ETA:
0s
info: installing component
'cargo'
info: installing component
'clippy'
info: installing component
'rust-docs'
18.8
MiB
/
18.8
MiB (
100
%
)
1.9
MiB
/
s
in
6s
ETA:
0s
info: installing component
'rust-std'
22.9
MiB
/
22.9
MiB (
100
%
)
10.5
MiB
/
s
in
2s
ETA:
0s
info: installing component
'rustc'
65.2
MiB
/
65.2
MiB (
100
%
)
12.2
MiB
/
s
in
5s
ETA:
0s
info: installing component
'rustfmt'
info: default toolchain
set
to
'stable-x86_64-pc-windows-msvc'
stable
-
x86_64
-
pc
-
windows
-
msvc installed
-
rustc
1.58
.
1
(db9d1b20b
2022
-
01
-
20
)
Rust
is
installed now. Great!
To get started you may need to restart your current shell.
This would
reload
its PATH environment variable to include
Cargo's
bin
directory (
%
USERPROFILE
%
\.cargo\
bin
).
Press the Enter key to
continue
.
命令 |
说明 |
备注 |
rustup doc |
打开官方指导文档 |
|
cargo new projectName |
创建一个rust工程 |
示例:cargo new firstRustProject |
cargo run |
运行rust工程 |
|
cargo build |
编译rust工程 |
若增加了依赖,即修改了toml文件,需要重新编译 |
fn main() {
println!(
"Hello, world!"
);
/
/
变量默认是不可变的,加上 mut 关键字就可以重新赋值。
let mut x
=
5
;
println!(
"The value of x is {} "
,x);
x
=
6
;
println!(
"The value of x is {} "
,x);
/
/
变量的隐藏
let money
=
100
;
println!(
"money is {}"
,money);
let money
=
money
+
8
;
println!(
"money is {}"
,money);
let money
=
"一百元"
;
println!(
"money is {}"
,money);
/
/
常量使用 const 关键字声明,声明的时候必须指定数据类型,常量名全大写。
/
/
不需要let , 不可使用mut 修饰
const MAX_PIONTS: u32
=
888
;
println!(
"The constant is {}"
,MAX_PIONTS);
let result:char
=
a_function(
88
,
'M'
, false);
println!(
"result is {}"
,result);
}
fn a_function(a:u64,b:char,c:
bool
)
-
> char{
println!(
"a is {}"
,a);
println!(
"b is {}"
,b);
println!(
"c is {}"
,c);
return
'N'
;
}
fn main() {
println!(
"Hello, world!"
);
/
/
变量默认是不可变的,加上 mut 关键字就可以重新赋值。
let mut x
=
5
;
println!(
"The value of x is {} "
,x);
x
=
6
;
println!(
"The value of x is {} "
,x);
/
/
变量的隐藏
let money
=
100
;
println!(
"money is {}"
,money);
let money
=
money
+
8
;
println!(
"money is {}"
,money);
let money
=
"一百元"
;
println!(
"money is {}"
,money);
/
/
常量使用 const 关键字声明,声明的时候必须指定数据类型,常量名全大写。
/
/
不需要let , 不可使用mut 修饰
const MAX_PIONTS: u32
=
888
;
println!(
"The constant is {}"
,MAX_PIONTS);
let result:char
=
a_function(
88
,
'M'
, false);
println!(
"result is {}"
,result);
}
fn a_function(a:u64,b:char,c:
bool
)
-
> char{
println!(
"a is {}"
,a);
println!(
"b is {}"
,b);
println!(
"c is {}"
,c);
return
'N'
;
}
Hello, world!
The value of x
is
5
The value of x
is
6
money
is
100
money
is
108
money
is
一百元
The constant
is
888
a
is
88
b
is
M
c
is
false
result
is
N
Hello, world!
The value of x
is
5
The value of x
is
6
money
is
100
money
is
108
money
is
一百元
The constant
is
888
a
is
88
b
is
M
c
is
false
result
is
N
fn main() {
println!(
"Hello, world!"
);
let q
=
3.0
;
let q:f32
=
5.00
;
let w
=
true;
let r:
bool
=
false;
let t
=
'';
let tup :(i32,u64,
bool
)
=
(
88
,
99
,false);
println!(
"元素1:{},元素2:{},元素3:{}"
,tup.
0
, tup.
1
, tup.
2
);
let arr:[u64;
5
]
=
[
1
,
2
,
3
,
5
,
5
];
let arr2
=
[
'E'
;
9
];
println!(
"arr piont 2 is :{}"
,arr[
1
]);
println!(
"arr2 piont 2 is :{}"
,arr2[
1
]);
/
/
Vector:todo
}
fn main() {
println!(
"Hello, world!"
);
let q
=
3.0
;
let q:f32
=
5.00
;
let w
=
true;
let r:
bool
=
false;
let t
=
'';
let tup :(i32,u64,
bool
)
=
(
88
,
99
,false);
println!(
"元素1:{},元素2:{},元素3:{}"
,tup.
0
, tup.
1
, tup.
2
);
let arr:[u64;
5
]
=
[
1
,
2
,
3
,
5
,
5
];
let arr2
=
[
'E'
;
9
];
println!(
"arr piont 2 is :{}"
,arr[
1
]);
println!(
"arr2 piont 2 is :{}"
,arr2[
1
]);
/
/
Vector:todo
}
fn main() {
println!(
"Hello, world!"
);
/
/
条件语句
let a
=
12
;
let b;
if
a >
0
{
b
=
1
;
}
else
if
a <
0
{
b
=
-
1
;
}
else
{
b
=
0
;
}
println!(
"b is {}"
, b);
/
/
三元运算符
let x
=
3
;
let number
=
if
x >
0
{
1
}
else
{
-
1
};
println!(
"number 为 {}"
, number);
}
fn main() {
println!(
"Hello, world!"
);
/
/
条件语句
let a
=
12
;
let b;
if
a >
0
{
b
=
1
;
}
else
if
a <
0
{
b
=
-
1
;
}
else
{
b
=
0
;
}
println!(
"b is {}"
, b);
/
/
三元运算符
let x
=
3
;
let number
=
if
x >
0
{
1
}
else
{
-
1
};
println!(
"number 为 {}"
, number);
}
fn main() {
println!(
"Hello, world!"
);
/
/
循环
/
/
while
let mut number
=
1
;
while
number !
=
4
{
println!(
"{}"
, number);
number
+
=
1
;
}
println!(
"while cycle EXIT"
);
/
/
for
-
迭代器
let a
=
[
10
,
20
,
30
,
40
,
50
];
for
i
in
a.
iter
() {
println!(
"元素值为 : {}"
, i);
}
println!(
"for-iter cycle EXIT"
);
/
/
for
-
下标
let b
=
[
10
,
20
,
30
,
40
,
50
];
let mut length
=
b.
len
();
println!(
"b 数组的长度是:{}"
, length);
for
i
in
0.
.length {
println!(
"b[{}] = {}"
, i, b[i]);
}
println!(
"for-index cycle EXIT"
);
/
/
loop 终止循环,并返回一个值
let s
=
[
'R'
,
'U'
,
'N'
,
'O'
,
'B'
];
let mut i
=
0
;
let location
=
loop {
let ch
=
s[i];
if
ch
=
=
'B'
{
break
i;
}
i
+
=
1
;
};
println!(
" \'B\' 的索引为 {}"
, location);
}
fn main() {
println!(
"Hello, world!"
);
/
/
循环
/
/
while
let mut number
=
1
;
while
number !
=
4
{
println!(
"{}"
, number);
number
+
=
1
;
}
println!(
"while cycle EXIT"
);
/
/
for
-
迭代器
let a
=
[
10
,
20
,
30
,
40
,
50
];
for
i
in
a.
iter
() {
println!(
"元素值为 : {}"
, i);
}
println!(
"for-iter cycle EXIT"
);
/
/
for
-
下标
let b
=
[
10
,
20
,
30
,
40
,
50
];
let mut length
=
b.
len
();
println!(
"b 数组的长度是:{}"
, length);
for
i
in
0.
.length {
println!(
"b[{}] = {}"
, i, b[i]);
}
println!(
"for-index cycle EXIT"
);
/
/
loop 终止循环,并返回一个值
let s
=
[
'R'
,
'U'
,
'N'
,
'O'
,
'B'
];
let mut i
=
0
;
let location
=
loop {
let ch
=
s[i];
if
ch
=
=
'B'
{
break
i;
}
i
+
=
1
;
};
println!(
" \'B\' 的索引为 {}"
, location);
}
println!(
"print a small =_=*"
);
println!(
"print a small =_=*"
);
let mut name
=
String::
from
(
"cml"
);
println!(
"输出中带花括号:{{ {} }}"
, name);
let mut name
=
String::
from
(
"cml"
);
println!(
"输出中带花括号:{{ {} }}"
, name);
输出中带花括号:{ cml }
println!(
"输出一个结构体,a={:?}"
, a);
println!(
"输出一个结构体,a={:?}"
, a);
let mut guess
=
String::new();
io::stdin().read_line(&mut guess).expect(
"无法读取行"
);
let mut guess
=
String::new();
io::stdin().read_line(&mut guess).expect(
"无法读取行"
);
fn main() {
println!(
"Hello, world!"
);
/
/
01.
基本数据类型(值类型)变量在栈空间中可以复制。先给x赋值
9
(let x
=
9
),将x赋值给y等同于直接给y赋值
9
(let y
=
x 等同于let y
=
9
)
let x
=
9
;
let y
=
x;
/
/
x
is
:
9
, y
is
:
9
println!(
"x is :{0} , y is :{1}"
, x, y);
let c1
=
'M'
;
let c2
=
c1;
/
/
c1
is
:M , c2
is
:M
println!(
"c1 is :{0} , c2 is :{1}"
, c1, c2);
/
/
02.
引用类型变量在堆空间中的“值引用”可以复制,但是存储在栈空间的“值”不可复制。因此,引用类型变量仅可被消费一次。
let s1
=
String::
from
(
"hello"
);
let s2
=
s1;
/
/
编译错误:use of moved value: `s1`
/
/
let s3
=
s1;
/
/
编译错误:borrow of moved value: `s1`
/
/
println!(
"s1 is :{0} , s2 is :{1}"
, s1, s2);
/
/
03.
引用类型变量可以通过“克隆”的方式复制。
let h1
=
String::
from
(
"hello"
);
let h2
=
h1.clone();
let h3
=
h1.clone();
/
/
h1
=
hello, h2
=
hello , h3
is
:hello
println!(
"h1 = {}, h2 = {} , h3 is :{}"
, h1, h2, h3);
/
/
04.
特殊的数据类型
-
-
-
>类型的引用(即指针),使用 & 关键字表示
let k1
=
String::
from
(
"hello"
);
/
/
k1为String类型:std::String::String ; k2 为带String类型的指针类型:&std::String:String
let k2
=
&k1;
let k3
=
k2;
/
/
指针存放在栈中,便于理解可以将指针看作“特殊的值类型”。所以虽然k2已经赋值给了k3,任然可以赋值给k4。
let k4
=
k2;
/
/
k1无法赋值给k5,因为k1已经被借用了。这是出于安全考虑,试想如果多个人都可以借用k1,意味着多个人可以修改k1,那势必对正在使用它的人正在进行的工作产生影响
/
/
let k5
=
k1;
/
/
编译错误:`k2`
is
a `&` reference, so the data it refers to cannot be borrowed as mutable
/
/
既然k2是指针类型,那么就不允许被修改,因为k2的“值”本身是借来的,如果修改了,那么势必对正在使用它的人正在进行的工作产生影响
/
/
k2.push_str(
"world"
);
println!(
"k1 is {}, k2 is {}, k3 is :{} , k4 is : {}"
, k1, k2, k3, k4);
/
/
值类型(基本数据类型)变量也可以使用指针,但是一般不建议这样使用
/
/
n1为i32类型;n2为&i32类型
let n1
=
8
;
let n2
=
&n1;
println!(
"n1 is :{} , n2 is :{}"
, n1, n2);
/
/
05.
可变变量的指针。
let mut m1
=
String::
from
(
"run"
);
let m2
=
&mut m1;
m2.push_str(
",world"
);
println!(
"m2 is :{}"
, m2);
/
/
编译错误:cannot borrow `m1` as immutable because it
is
also borrowed as mutable
/
/
借出去后,所有权已不在拥有,所以无法被消费
/
/
println!(
"m1 is :{} , m2 is :{}"
, m1, m2);
/
/
编译错误:cannot borrow `m2` as mutable, as it
is
not
declared as mutable。cannot borrow as mutable
/
/
不可将借来的东西再借给别人
/
/
let m3
=
&mut m2;
/
/
06.rust
中不允许出现空指针
}
fn main() {
println!(
"Hello, world!"
);
/
/
01.
基本数据类型(值类型)变量在栈空间中可以复制。先给x赋值
9
(let x
=
9
),将x赋值给y等同于直接给y赋值
9
(let y
=
x 等同于let y
=
9
)
let x
=
9
;
let y
=
x;
/
/
x
is
:
9
, y
is
:
9
println!(
"x is :{0} , y is :{1}"
, x, y);
let c1
=
'M'
;
let c2
=
c1;
/
/
c1
is
:M , c2
is
:M
println!(
"c1 is :{0} , c2 is :{1}"
, c1, c2);
/
/
02.
引用类型变量在堆空间中的“值引用”可以复制,但是存储在栈空间的“值”不可复制。因此,引用类型变量仅可被消费一次。
let s1
=
String::
from
(
"hello"
);
let s2
=
s1;
/
/
编译错误:use of moved value: `s1`
/
/
let s3
=
s1;
/
/
编译错误:borrow of moved value: `s1`
/
/
println!(
"s1 is :{0} , s2 is :{1}"
, s1, s2);
/
/
03.
引用类型变量可以通过“克隆”的方式复制。
let h1
=
String::
from
(
"hello"
);
let h2
=
h1.clone();
let h3
=
h1.clone();
/
/
h1
=
hello, h2
=
hello , h3
is
:hello
println!(
"h1 = {}, h2 = {} , h3 is :{}"
, h1, h2, h3);
/
/
04.
特殊的数据类型
-
-
-
>类型的引用(即指针),使用 & 关键字表示
let k1
=
String::
from
(
"hello"
);
/
/
k1为String类型:std::String::String ; k2 为带String类型的指针类型:&std::String:String
let k2
=
&k1;
let k3
=
k2;
/
/
指针存放在栈中,便于理解可以将指针看作“特殊的值类型”。所以虽然k2已经赋值给了k3,任然可以赋值给k4。
let k4
=
k2;
/
/
k1无法赋值给k5,因为k1已经被借用了。这是出于安全考虑,试想如果多个人都可以借用k1,意味着多个人可以修改k1,那势必对正在使用它的人正在进行的工作产生影响
/
/
let k5
=
k1;
/
/
编译错误:`k2`
is
a `&` reference, so the data it refers to cannot be borrowed as mutable
/
/
既然k2是指针类型,那么就不允许被修改,因为k2的“值”本身是借来的,如果修改了,那么势必对正在使用它的人正在进行的工作产生影响
/
/
k2.push_str(
"world"
);
println!(
"k1 is {}, k2 is {}, k3 is :{} , k4 is : {}"
, k1, k2, k3, k4);
/
/
值类型(基本数据类型)变量也可以使用指针,但是一般不建议这样使用
/
/
n1为i32类型;n2为&i32类型
let n1
=
8
;
let n2
=
&n1;
println!(
"n1 is :{} , n2 is :{}"
, n1, n2);
/
/
05.
可变变量的指针。
let mut m1
=
String::
from
(
"run"
);
let m2
=
&mut m1;
m2.push_str(
",world"
);
println!(
"m2 is :{}"
, m2);
/
/
编译错误:cannot borrow `m1` as immutable because it
is
also borrowed as mutable
/
/
借出去后,所有权已不在拥有,所以无法被消费
/
/
println!(
"m1 is :{} , m2 is :{}"
, m1, m2);
/
/
编译错误:cannot borrow `m2` as mutable, as it
is
not
declared as mutable。cannot borrow as mutable
/
/
不可将借来的东西再借给别人
/
/
let m3
=
&mut m2;
/
/
06.rust
中不允许出现空指针
}
Hello, world!
x
is
:
9
, y
is
:
9
c1
is
:M , c2
is
:M
h1
=
hello, h2
=
hello , h3
is
:hello
k1
is
hello, k2
is
hello, k3
is
:hello , k4
is
: hello
n1
is
:
8
, n2
is
:
8
m2
is
:run,world
Hello, world!
x
is
:
9
, y
is
:
9
c1
is
:M , c2
is
:M
h1
=
hello, h2
=
hello , h3
is
:hello
k1
is
hello, k2
is
hello, k3
is
:hello , k4
is
: hello
n1
is
:
8
, n2
is
:
8
m2
is
:run,world
fn main() {
println!(
"Hello, world!"
);
let s
=
String::
from
(
"hello,world."
);
/
/
ss 的数据类型为:&
str
let ss
=
&s[
0.
.
5
];
println!(
"s is : {} , ss is : {}"
, s, ss);
let arr
=
[
1
,
3
,
5
,
7
,
9
];
/
/
start_part 的数据类型为:&[i32]
let start_part
=
&arr[
0.
.
3
];
let end_part
=
&arr[
3.
.];
let full_part
=
&arr[..];
println!(
"arr is : {:?} , start_part is : {:?} , end_part is : {:?} , full_part is : {:?}"
,
arr, start_part, end_part, full_part
);
}
fn main() {
println!(
"Hello, world!"
);
let s
=
String::
from
(
"hello,world."
);
/
/
ss 的数据类型为:&
str
let ss
=
&s[
0.
.
5
];
println!(
"s is : {} , ss is : {}"
, s, ss);
let arr
=
[
1
,
3
,
5
,
7
,
9
];
/
/
start_part 的数据类型为:&[i32]
let start_part
=
&arr[
0.
.
3
];
let end_part
=
&arr[
3.
.];
let full_part
=
&arr[..];
println!(
"arr is : {:?} , start_part is : {:?} , end_part is : {:?} , full_part is : {:?}"
,
arr, start_part, end_part, full_part
);
}
Hello, world!
s
is
: hello,world. , ss
is
: hello
arr
is
: [
1
,
3
,
5
,
7
,
9
] , start_part
is
: [
1
,
3
,
5
] , end_part
is
: [
7
,
9
] , full_part
is
: [
1
,
3
,
5
,
7
,
9
]
Hello, world!
s
is
: hello,world. , ss
is
: hello
arr
is
: [
1
,
3
,
5
,
7
,
9
] , start_part
is
: [
1
,
3
,
5
] , end_part
is
: [
7
,
9
] , full_part
is
: [
1
,
3
,
5
,
7
,
9
]
/
*
*
*
人
*
/
struct Person {
/
/
结构体属性:声明方式,属性名:数据类型;即使是最后一个属性,末尾也要加英文逗号;属性可以是另外一个结构体,但不能是本身;属性可以是一个元组
active:
bool
,
name: String,
email: String,
sign_in_count: u64,
nation: Nation,
parent: Parent,
empty: Empty,
tuple
: (u32, u32),
}
/
*
*
*
国家结构体
*
/
struct Nation {
name: String,
area: String,
time_zone: u64,
}
/
*
*
*
父母结构体
*
特殊的结构体,元组结构体。
*
/
struct Parent(String, String);
/
*
*
*
空结构体
*
/
struct Empty;
fn main() {
println!(
"Hello, world!"
);
/
/
实例化一个结构体
let p1
=
Person {
active: true,
name: String::
from
(
"cml"
),
email: String::
from
(
"cnaylor@163.com"
),
sign_in_count:
99
,
nation: Nation {
name: String::
from
(
"xm"
),
area: String::
from
(
"north"
),
time_zone:
8
,
},
parent: Parent(String::
from
(
"baba"
), String::
from
(
"mama"
)),
empty: Empty,
tuple
: (
8
,
8
),
};
/
/
实例化一个可变结构体
let mut p2
=
Person {
active: true,
name: String::
from
(
"cml"
),
email: String::
from
(
"cnaylor@163.com"
),
sign_in_count:
99
,
nation: Nation {
name: String::
from
(
"xm"
),
area: String::
from
(
"north"
),
time_zone:
8
,
},
parent: Parent(String::
from
(
"baba"
), String::
from
(
"mama"
)),
empty: Empty,
tuple
: (
8
,
8
),
};
/
/
给结构体实例重新赋值
p2.email
=
String::
from
(
"c@163.com"
);
let email
=
&p2.email;
println!(
"p1 is : {:#?} , p2 is : {:#?} , email is :{}"
, p1, p2, email);
struct User {
active:
bool
,
username: String,
email: String,
sign_in_count: u64,
}
fn build_user(email: String, username: String)
-
> User {
let u
=
User {
email: email,
/
/
简写。函数参数和结构体属性名相同,可简写
/
/
email,
username,
active: true,
sign_in_count:
1
,
};
/
/
从已创建的结构体实例创建实例
/
/
let u2
=
User { ..u };
let u3
=
User {
email: String::
from
(
"o@163.com"
),
..u
};
return
u3;
}
let u
=
build_user(String::
from
(
"a@163.com"
), String::
from
(
"tom"
));
println!(
"u is : {:?}"
, u);
}
/
*
*
*
人
*
/
struct Person {
/
/
结构体属性:声明方式,属性名:数据类型;即使是最后一个属性,末尾也要加英文逗号;属性可以是另外一个结构体,但不能是本身;属性可以是一个元组
active:
bool
,
name: String,
email: String,
sign_in_count: u64,
nation: Nation,
parent: Parent,
empty: Empty,
tuple
: (u32, u32),
}
/
*
*
*
国家结构体
*
/
struct Nation {
name: String,
area: String,
time_zone: u64,
}
/
*
*
*
父母结构体
*
特殊的结构体,元组结构体。
*
/
struct Parent(String, String);
/
*
*
*
空结构体
*
/
struct Empty;
fn main() {
println!(
"Hello, world!"
);
/
/
实例化一个结构体
let p1
=
Person {
active: true,
name: String::
from
(
"cml"
),
email: String::
from
(
"cnaylor@163.com"
),
sign_in_count:
99
,
nation: Nation {
name: String::
from
(
"xm"
),
area: String::
from
(
"north"
),
time_zone:
8
,
},
parent: Parent(String::
from
(
"baba"
), String::
from
(
"mama"
)),
empty: Empty,
tuple
: (
8
,
8
),
};
/
/
实例化一个可变结构体
let mut p2
=
Person {
active: true,
name: String::
from
(
"cml"
),
email: String::
from
(
"cnaylor@163.com"
),
sign_in_count:
99
,
nation: Nation {
name: String::
from
(
"xm"
),
area: String::
from
(
"north"
),
time_zone:
8
,
},
parent: Parent(String::
from
(
"baba"
), String::
from
(
"mama"
)),
empty: Empty,
tuple
: (
8
,
8
),
};
/
/
给结构体实例重新赋值
p2.email
=
String::
from
(
"c@163.com"
);
let email
=
&p2.email;
println!(
"p1 is : {:#?} , p2 is : {:#?} , email is :{}"
, p1, p2, email);
struct User {
active:
bool
,
username: String,
email: String,
sign_in_count: u64,
}
fn build_user(email: String, username: String)
-
> User {
let u
=
User {
email: email,
/
/
简写。函数参数和结构体属性名相同,可简写
/
/
email,
username,
active: true,
sign_in_count:
1
,
};
/
/
从已创建的结构体实例创建实例
/
/
let u2
=
User { ..u };
let u3
=
User {
email: String::
from
(
"o@163.com"
),
..u
};
return
u3;
}
let u
=
build_user(String::
from
(
"a@163.com"
), String::
from
(
"tom"
));
println!(
"u is : {:?}"
, u);
}
Hello, world!
p1
is
: Person {
active: true,
name:
"cml"
,
email:
"cnaylor@163.com"
,
sign_in_count:
99
,
nation: Nation {
time_zone:
8
,
},
parent: Parent(
"baba"
,
"mama"
,
),
empty: Empty,
tuple
: (
8
,
8
,
),
} , email
is
:c@
163.com
u
is
: User { active: true, username:
"tom"
, email:
"o@163.com"
, sign_in_count:
1
}
Hello, world!
p1
is
: Person {
active: true,
name:
"cml"
,
email:
"cnaylor@163.com"
,
sign_in_count:
99
,
nation: Nation {
time_zone:
8
,
},
parent: Parent(
"baba"
,
"mama"
,
),
empty: Empty,
tuple
: (
8
,
8
,
),
} , email
is
:c@
163.com
u
is
: User { active: true, username:
"tom"
, email:
"o@163.com"
, sign_in_count:
1
}
use std::fs::
File
;
fn main() {
let f
=
File
::
open
(
"hello.txt"
);
match f {
Ok(
file
)
=
> {
println!(
"File opened successfully."
);
},
Err(err)
=
> {
println!(
"Failed to open the file."
);
}
}
}
use std::fs::
File
;
fn main() {
let f
=
File
::
open
(
"hello.txt"
);
match f {
Ok(
file
)
=
> {
println!(
"File opened successfully."
);
},
Err(err)
=
> {
println!(
"Failed to open the file."
);
}
}
}
fn main() {
enum Book {
Papery(u32),
Electronic(String)
}
let book
=
Book::Electronic(String::
from
(
"url"
));
if
let Book::Papery(index)
=
book {
println!(
"Papery {}"
, index);
}
else
{
println!(
"Not papery book"
);
}
}
fn main() {
enum Book {
Papery(u32),
Electronic(String)
}
let book
=
Book::Electronic(String::
from
(
"url"
));
if
let Book::Papery(index)
=
book {
println!(
"Papery {}"
, index);
}
else
{
println!(
"Not papery book"
);
}
}
enum Option<T> {
Some(T),
None
,}
enum Option<T> {
Some(T),
None
,}
/
/
整数除法。
fn checked_division(dividend: i32, divisor: i32)
-
> Option<i32> {
if
divisor
=
=
0
{
/
/
失败表示成 `
None
` 取值
None
}
else
{
/
/
结果 Result 被包装到 `Some` 取值中
Some(dividend
/
divisor)
}
}
/
/
此函数处理可能失败的除法
fn try_division(dividend: i32, divisor: i32) {
/
/
`Option` 值可以进行模式匹配,就和其他枚举类型一样
let result
=
checked_division(dividend, divisor);
if
result!
=
Option::
None
{
/
/
获取返回值
println!(
"Nice, result is :{:?}"
, result.unwrap());
}
match checked_division(dividend, divisor) {
None
=
> println!(
"{} / {} failed!"
, dividend, divisor),
Some(quotient)
=
> {
println!(
"{} / {} = {}"
, dividend, divisor, quotient)
}
}
}
fn main() {
try_division(
4
,
2
);
try_division(
1
,
0
);
}
/
/
整数除法。
fn checked_division(dividend: i32, divisor: i32)
-
> Option<i32> {
if
divisor
=
=
0
{
/
/
失败表示成 `
None
` 取值
None
}
else
{
/
/
结果 Result 被包装到 `Some` 取值中
Some(dividend
/
divisor)
}
}
/
/
此函数处理可能失败的除法
fn try_division(dividend: i32, divisor: i32) {
/
/
`Option` 值可以进行模式匹配,就和其他枚举类型一样
let result
=
checked_division(dividend, divisor);
if
result!
=
Option::
None
{
/
/
获取返回值
println!(
"Nice, result is :{:?}"
, result.unwrap());
}
match checked_division(dividend, divisor) {
None
=
> println!(
"{} / {} failed!"
, dividend, divisor),
Some(quotient)
=
> {
println!(
"{} / {} = {}"
, dividend, divisor, quotient)
}
}
}
fn main() {
try_division(
4
,
2
);
try_division(
1
,
0
);
}
Nice, result
is
:
2
4
/
2
=
2
1
/
0
failed!
Nice, result
is
:
2
4
/
2
=
2
1
/
0
failed!
fn main() {
println!(
"Hello, world!"
);
/
/
新建一个 vector
let mut v: Vec<i32>
=
Vec::new();
let v2
=
vec![
1
,
2
,
3
];
println!(
"v is : {:?} , v2 is : {:?}"
, v, v2);
/
/
更新
v.push(
888
);
v.push(
111
);
v.push(
222
);
v.push(
333
);
/
/
删除
v.remove(
0
);
/
/
查询
let two
=
v[
1
];
let two2
=
&v[
1
];
/
/
get方法返回的是Option
let three
=
v.get(
2
);
println!(
"two is : {} , three is : {:?}"
, two, three);
/
/
遍历
let arr
=
vec![
100
,
32
,
57
];
for
i
in
&arr {
println!(
"arr item for --> {}"
, i);
}
/
/
借助枚举,vector中可以存储不同的数据类型
let row
=
vec![
SpreadsheetCell::
Int
(
3
),
SpreadsheetCell::Text(String::
from
(
"blue"
)),
SpreadsheetCell::
Float
(
10.12
),
];
let s
=
&row[
1
];
println!(
"row is : {:?} , s is : {:?} "
, row, s);
}
enum SpreadsheetCell {
Int
(i32),
Float
(f64),
Text(String),
}
fn main() {
println!(
"Hello, world!"
);
/
/
新建一个 vector
let mut v: Vec<i32>
=
Vec::new();
let v2
=
vec![
1
,
2
,
3
];
println!(
"v is : {:?} , v2 is : {:?}"
, v, v2);
/
/
更新
v.push(
888
);
v.push(
111
);
v.push(
222
);
v.push(
333
);
/
/
删除
v.remove(
0
);
/
/
查询
let two
=
v[
1
];
let two2
=
&v[
1
];
/
/
get方法返回的是Option
let three
=
v.get(
2
);
println!(
"two is : {} , three is : {:?}"
, two, three);
/
/
遍历
let arr
=
vec![
100
,
32
,
57
];
for
i
in
&arr {
println!(
"arr item for --> {}"
, i);
}
/
/
借助枚举,vector中可以存储不同的数据类型
let row
=
vec![
SpreadsheetCell::
Int
(
3
),
SpreadsheetCell::Text(String::
from
(
"blue"
)),
SpreadsheetCell::
Float
(
10.12
),
];
let s
=
&row[
1
];
println!(
"row is : {:?} , s is : {:?} "
, row, s);
}
enum SpreadsheetCell {
Int
(i32),
Float
(f64),
Text(String),
}
Hello, world!
v
is
: [] , v2
is
: [
1
,
2
,
3
]
two
is
:
222
, three
is
: Some(
333
)
arr item
for
-
-
>
100
arr item
for
-
-
>
32
arr item
for
-
-
>
57
row
is
: [
Int
(
3
), Text(
"blue"
),
Float
(
10.12
)] , s
is
: Text(
"blue"
)
Hello, world!
v
is
: [] , v2
is
: [
1
,
2
,
3
]
two
is
:
222
, three
is
: Some(
333
)
arr item
for
-
-
>
100
arr item
for
-
-
>
32
arr item
for
-
-
>
57
row
is
: [
Int
(
3
), Text(
"blue"
),
Float
(
10.12
)] , s
is
: Text(
"blue"
)
fn main() {
println!(
"Hello, world!"
);
/
/
创建
let mut s
=
String::new();
let data
=
"initial contents"
;
let ss
=
data.to_string();
let sss
=
String::
from
(
"你好!"
);
println!(
"s is : {} , ss is : {} , sss is : {}"
, s, ss, sss);
/
/
更新
let mut word
=
String::
from
(
"Aa"
);
word.push_str(
"Bb"
);
word.push(
'C'
);
word
+
=
"cDd"
;
word
=
format
!(
"{}{}-{}"
, word,
"Ee"
,
"Ff"
);
println!(
"final word is : {} "
, word);
/
/
不要使用下标访问
let p
=
String::
from
(
"hello"
);
let p1
=
&p[
0.
.
3
];
let k
=
String::
from
(
"你好我是陈明亮"
);
let k1
=
&k[
0.
.
3
];
/
/
输出结果可能不是大多数人预期:p1
is
: hel , k1
is
: 你
println!(
"p1 is : {} , k1 is : {}"
, p1, k1);
/
/
遍历
word
+
=
"北京"
;
for
i
in
word.chars() {
println!(
"word ---> item is :{}"
, i);
}
}
fn main() {
println!(
"Hello, world!"
);
/
/
创建
let mut s
=
String::new();
let data
=
"initial contents"
;
let ss
=
data.to_string();
let sss
=
String::
from
(
"你好!"
);
println!(
"s is : {} , ss is : {} , sss is : {}"
, s, ss, sss);
/
/
更新
let mut word
=
String::
from
(
"Aa"
);
word.push_str(
"Bb"
);
word.push(
'C'
);
word
+
=
"cDd"
;
word
=
format
!(
"{}{}-{}"
, word,
"Ee"
,
"Ff"
);
println!(
"final word is : {} "
, word);
/
/
不要使用下标访问
let p
=
String::
from
(
"hello"
);
let p1
=
&p[
0.
.
3
];
let k
=
String::
from
(
"你好我是陈明亮"
);
let k1
=
&k[
0.
.
3
];
/
/
输出结果可能不是大多数人预期:p1
is
: hel , k1
is
: 你
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课
最后于 2022-12-27 16:32
被kanxue编辑
,原因: