2022-10-16
Rust
00
请注意,本文编写于 650 天前,最后修改于 527 天前,其中某些信息可能已经过时。

目录

rust的第一天
2022-10-16 02:11
1. rust的安装
2. rust的hello world
3. rust的数据类型
3.1 标量
3.1.1 整型
3.1.2 浮点型
3.1.3 布尔型
3.1.4 字符型
3.2 复合类型
3.2.1 元组
3.2.2 数组
3.2.3 字符串
3. 变量
3.1 变量的声明
3.2 变量的修改
3.3 变量的隐藏
3.4 常量
4. 函数
4.1 函数的声明
4.2 函数的参数
4.3 函数的返回值
5. 注释
6. 运算符
6.1 算术运算符
6.2 比较运算符
6.3 逻辑运算符
6.4 赋值运算符
6.5 位运算符
6.6 其他运算符

rust的第一天

2022-10-16 02:11

1. rust的安装

关于rust的安装,可以参考官方文档。 因为我的系统为manjaro,所以我选择了使用yay来安装rust。

bash
yay -S rust

安装完成后,可以使用rustc --version来查看rust的版本。

2. rust的hello world

使用cargo来创建一个新的项目。

bash
cargo new rust1

在rust中,使用println!来输出字符串。

rust
fn main() { println!("Hello, world!"); }

使用cargo run来运行程序。

bash
cargo run

输出结果:

bash
Compiling rust1 v0.1.0 (/home/duty/rust/rust1) Finished dev [unoptimized + debuginfo] target(s) in 0.40s Running `target/debug/rust1` Hello, world!

3. rust的数据类型

rust的数据类型分为两类:标量和复合。

3.1 标量

标量类型包括整型、浮点型、布尔型和字符型。

3.1.1 整型

大小有符号无符号
8-biti8u8
16-biti16u16
32-biti32u32
64-biti64u64
archisizeusize

值得注意的是,rust数据类型和go一样是自动推断的,所以在声明变量的时候,可以不用指定类型。 但是,如果想要指定类型,可以使用let x: i32 = 5;来指定类型。

  1. 如果你确定了类型,但类型不匹配,rust会报错。
rust
Compiling rust1 v0.1.0 (/home/duty/rust/rust1) error[E0600]: cannot apply unary operator `-` to type `u32` --> src/main.rs:11:19 | 11 | let x : u32 = -67; | ^^^ cannot apply unary operator `-` | = note: unsigned values cannot be negated For more information about this error, try `rustc --explain E0600`. error: could not compile `rust1` due to previous error
  1. 如果i8的数据类型,我们给他赋值为129,rust会报错。
rust
Compiling rust1 v0.1.0 (/home/duty/rust/rust1) error: literal out of range for `i8` --> src/main.rs:11:16 | 11 | let x:i8 = 129; | ^^^ | = note: `#[deny(overflowing_literals)]` on by default = note: the literal `129` does not fit into the type `i8` whose range is `-128..=127` = help: consider using the type `u8` instead error: could not compile `rust1` due to previous error
  1. 不同大小的整形的长度
rust
Compiling rust1 v0.1.0 (/home/duty/rust/rust1) Finished dev [unoptimized + debuginfo] target(s) in 0.30s Running `target/debug/rust1` i8的最大值: 127 i8的最小值: -128 i16的最大值: 32767 i16的最小值: -32768 i32的最大值: 2147483647 i32的最小值: -2147483648 i64的最大值: 9223372036854775807 i64的最小值: -9223372036854775808 i128的最大值: 170141183460469231731687303715884105727 i128的最小值: -170141183460469231731687303715884105728

3.1.2 浮点型

rust的浮点型包括f32和f64。

rust
let x:f32 = 889_990_3.0; println!("x = {}",x); println!("f32的最大值: {}",std::f32::MAX); println!("f32的最小值: {}",std::f32::MIN); println!("f64的最大值: {}",std::f64::MAX); println!("f64的最小值: {}",std::f64::MIN);

如上,我们可以使用_来分隔数字,使得数字更加易读。 输出结果:

bash
Compiling rust1 v0.1.0 (/home/duty/rust/rust1) Finished dev [unoptimized + debuginfo] target(s) in 0.30s Running `target/debug/rust1` x = 8899903 f32的最大值: 340282350000000000000000000000000000000 f32的最小值: -340282350000000000000000000000000000000 f64的最大值: 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 f64的最小值: -179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

3.1.3 布尔型

rust的布尔型只有true和false。

rust
let x = true; let y:bool = false; println!("x = {},y = {}",x,y);

输出结果:

bash
Compiling rust1 v0.1.0 (/home/duty/rust/rust1) Finished dev [unoptimized + debuginfo] target(s) in 0.30s Running `target/debug/rust1` x = true,y = false

3.1.4 字符型

rust的字符型是char,它的长度是4个字节。也就是单个字符或者汉字的长度。 使用utf-8编码。

rust
let x = 'a'; let y:char = 'b'; println!("x = {},y = {}",x,y);

输出结果:

bash
Compiling rust1 v0.1.0 (/home/duty/rust/rust1) Finished dev [unoptimized + debuginfo] target(s) in 0.30s Running `target/debug/rust1` x = a,y = b

3.2 复合类型

3.2.1 元组

元组是一个有序的列表,可以存储不同类型的数据。

rust
let x = (1,2,3); let y = (1,"hello",true); println!("x = {:?}",x); println!("y = {:?}",y);

输出结果:

bash
Compiling rust1 v0.1.0 (/home/duty/rust/rust1) Finished dev [unoptimized + debuginfo] target(s) in 0.30s Running `target/debug/rust1` x = (1, 2, 3) y = (1, "hello", true)

3.2.2 数组

数组是一个有序的列表,可以存储相同类型的数据。

rust
let x = [1,2,3]; let y = [1,2,3,4,5]; println!("x = {:?}",x); println!("y = {:?}",y);

输出结果:

bash
Compiling rust1 v0.1.0 (/home/duty/rust/rust1) Finished dev [unoptimized + debuginfo] target(s) in 0.30s Running `target/debug/rust1` x = [1, 2, 3] y = [1, 2, 3, 4, 5]

3.2.3 字符串

rust的字符串是String类型,它是一个可变的字符串。

rust
let mut x = String::from("hello"); x.push_str(" world"); println!("x = {}",x);

输出结果:

bash
Compiling rust1 v0.1.0 (/home/duty/rust/rust1) Finished dev [unoptimized + debuginfo] target(s) in 0.30s Running `target/debug/rust1` x = hello world

3. 变量

3.1 变量的声明

rust的变量声明使用let关键字。

rust
let x = 1; let y:i32 = 2; println!("x = {},y = {}",x,y);

输出结果:

bash
Compiling rust1 v0.1.0 (/home/duty/rust/rust1) Finished dev [unoptimized + debuginfo] target(s) in 0.30s Running `target/debug/rust1` x = 1,y = 2

3.2 变量的修改

rust的变量默认是不可变的,如果想要修改变量的值,需要使用mut关键字。

rust
let mut x = 1; x = 2; println!("x = {}",x);

输出结果:

bash
Compiling rust1 v0.1.0 (/home/duty/rust/rust1) Finished dev [unoptimized + debuginfo] target(s) in 0.30s Running `target/debug/rust1` x = 2

3.3 变量的隐藏

rust的变量可以隐藏,隐藏的变量会覆盖之前的变量。

rust
let x = 1; let x = 2; println!("x = {}",x);

输出结果:

bash
Compiling rust1 v0.1.0 (/home/duty/rust/rust1) Finished dev [unoptimized + debuginfo] target(s) in 0.30s Running `target/debug/rust1` x = 2

3.4 常量

rust的常量使用const关键字声明。

rust
const X:i32 = 1; const Y:i32 = 2; println!("x = {},y = {}",X,Y);

输出结果:

bash
Compiling rust1 v0.1.0 (/home/duty/rust/rust1) Finished dev [unoptimized + debuginfo] target(s) in 0.30s Running `target/debug/rust1` x = 1,y = 2

4. 函数

4.1 函数的声明

rust的函数使用fn关键字声明。

rust
fn add(x:i32,y:i32) -> i32{ x + y } fn main() { let x = 1; let y = 2; let z = add(x,y); println!("z = {}",z); }

输出结果:

bash
Compiling rust1 v0.1.0 (/home/duty/rust/rust1) Finished dev [unoptimized + debuginfo] target(s) in 0.30s Running `target/debug/rust1` z = 3

4.2 函数的参数

rust的函数参数可以有默认值。

rust
fn add(x:i32,y:i32 = 2) -> i32{ x + y } fn main() { let x = 1; let y = 2; let z = add(x); println!("z = {}",z); }

输出结果:

bash
Compiling rust1 v0.1.0 (/home/duty/rust/rust1) Finished dev [unoptimized + debuginfo] target(s) in 0.30s Running `target/debug/rust1` z = 3

4.3 函数的返回值

rust的函数可以有返回值,如果没有返回值,可以使用()表示。

rust
fn add(x:i32,y:i32) -> i32{ x + y } fn main() { let x = 1; let y = 2; let z = add(x,y); println!("z = {}",z); }

输出结果:

bash
Compiling rust1 v0.1.0 (/home/duty/rust/rust1) Finished dev [unoptimized + debuginfo] target(s) in 0.30s Running `target/debug/rust1` z = 3

5. 注释

rust的注释有两种,一种是单行注释,一种是多行注释。 单行注释使用//,多行注释使用/* */

rust
//单行注释 /* 多行注释 */

6. 运算符

6.1 算术运算符

运算符描述
+
-
*
/
%取余

6.2 比较运算符

运算符描述
==等于
!=不等于
>大于
<小于
>=大于等于
<=小于等于

6.3 逻辑运算符

运算符描述
&&逻辑与
||逻辑或
!逻辑非

6.4 赋值运算符

运算符描述
=赋值
+=加赋值
-=减赋值
*=乘赋值
/=除赋值
%=取余赋值

6.5 位运算符

运算符描述
&按位与
|按位或
^按位异或
~按位取反
<<左移
>>右移

6.6 其他运算符

运算符描述
.成员访问
->成员访问
::作用域
:类型标注
@模式匹配
..范围
..=范围
...范围
,分隔符
;分隔符
=>键值对
->返回类型
=>闭包
->闭包
如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:独语天涯

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!