Rust程序设计语言-官方文档
使用cargo创建一个项目,输出hello,world!
$cargo new hello
# hello.rs
fn main() {
println!("Hello, world!");
}
------------------
$cargo run hello
Compiling hello v0.1.0 (D:\RustPro\hello)
Finished dev [unoptimized + debuginfo] target(s) in 1.39s
Running `target\debug\hello.exe hello`
Hello, world!
rust语言和其他语言一样,也分常量和变量
- 常亮就是一直不变的,程序中不可以更改,使用const 进行定义
- 变量就是可变量,在Rust中分为可变变量和不可变变量
- 不可变变量使用 let 进行定义
-可变变量使用 let mut 进行定义
# 正确使用
fn main() {
const MAX_POINTS: u32 = 100_000;
println!("{}",MAX_POINTS);
}
输出:100000
# 错误使用,在程序中修改常量
fn main() {
const MAX_POINTS: u32 = 100_000;
MAX_POINTS = 200_000;
println!("{}",MAX_POINTS);
}
$cargo run hello
Compiling hello v0.1.0 (D:\RustPro\hello)
error[E0070]: invalid left-hand side of assignment
--> src\main.rs:3:16
|
3 | MAX_POINTS = 200_000;
| ---------- ^
| |
| cannot assign to this expression
For more information about this error, try `rustc --explain E0070`.
error: could not compile `hello` (bin "hello") due to previous error
# 不可变变量在程序中无法修改
fn main() {
let mut x:i64 = 6;
println!("{}",x);
x = 7;
println!("{}",x);
}
$cargo run hello
Compiling hello v0.1.0 (D:\RustPro\hello)
error[E0384]: cannot assign twice to immutable variable `x`
--> src\main.rs:4:5
|
2 | let x:i64 = 6;
| -
| |
| first assignment to `x`
| help: consider making this binding mutable: `mut x`
3 | println!("{}",x);
4 | x = 7;
| ^^^^^ cannot assign twice to immutable variable
For more information about this error, try `rustc --explain E0384`.
error: could not compile `hello` (bin "hello") due to previous error
# 应该使用可变变量
fn main() {
let mut x:i64 = 6;
println!("{}",x);
x = 7;
println!("{}",x);
}
$cargo run hello
Compiling hello v0.1.0 (D:\RustPro\hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.32s
Running `target\debug\hello.exe hello`
6
7
Rust 是 静态类型(statically typed)语言,也就是说在编译时就必须知道所有变量的类型。
与其他语言一样,Rust也有整型、浮点型、字符串、布尔型等数据类型,由于整型变量定义不规范容易造成溢出,所以单列一个表。另外在Rust语言中f32
代表单精度,f64
代表双精度。
长度 | 有符号 | 无符号 |
---|---|---|
8-bit | i8 | u8 |
16-bit | i16 | u16 |
32-bit | i32 | u32 |
64-bit | i64 | u64 |
128-bit | i128 | u128 |
arch | isize | usize |
与其他变成语言一样,单条件判断、多条件分支都可以。
# 多条件分支
fn main() {
let x:i64 = 6;
if x > 6{
println!(">6");
}else if x==6{
println!("=6");
}else {
println!("<6");
}
}
$cargo run hello
Compiling hello v0.1.0 (D:\RustPro\hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.74s
Running `target\debug\hello.exe hello`
=6
其他的条件判断语句按照以上例子拆解,不在赘述
match语句,它与其他编程语言中的switch或case语句相似,但功能更为强大和灵活。
fn main() {
let number = 3;
match number {
1 => println!("One"),
2 => println!("Two"),
3 => println!("Three"),
_ => println!("Another number"), // _ 是一个通配符,匹配所有其他情况
}
}
$cargo run hello
Compiling hello v0.1.0 (D:\RustPro\hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.35s
Running `target\debug\hello.exe hello`
Three
在Rust语言中,提供了三种循环语句
- loop 无条件循环
- while 条件循环
- for 集合遍历
- 由于loop是无条件循环语句,所以必须在循环内部加判断结束的语句,否则造成死循环。
- 使用break进行返回,可以携带返回值,可以跳转指定标签位置。
# break 携带返回值
fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
break counter * 2;
}
};
println!("The result is {result}");
}
$cargo run hello
Compiling hello v0.1.0 (D:\RustPro\hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.36s
Running `target\debug\hello.exe hello`
The result is 20
# loop多重循环,break 跳转指定标签
fn main() {
let mut count = 0;
'counting_up: loop {
println!("count = {count}");
let mut remaining = 10;
loop {
println!("remaining = {remaining}");
if remaining == 9 {
break;
}
if count == 2 {
break 'counting_up;
}
remaining -= 1;
}
count += 1;
}
println!("End count = {count}");
}
$cargo run hello
Compiling hello v0.1.0 (D:\RustPro\hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.35s
Running `target\debug\hello.exe hello`
count = 0
remaining = 10
remaining = 9
count = 1
remaining = 10
remaining = 9
count = 2
remaining = 10
End count = 2
与其他语言条件循环没有区别
# 输出100以内斐波那契数列
fn main() {
let mut a = 0;
let mut b = 1;
let mut c = 1;
while c < 100 {
println!("{}",c);
c = a+b;
a=b;
b=c;
}
}
$cargo run hello
Compiling hello v0.1.0 (D:\RustPro\hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.36s
Running `target\debug\hello.exe hello`
1
1
2
3
5
8
13
21
34
55
89
需要注意的是遍历的时候,需要的是集合下标还是集合的值。
# 单遍历集合值
fn main() {
let a = [10, 20, 30, 40, 50];
for element in a {
println!("the value is: {element}");
}
}
$cargo run hello
Compiling hello v0.1.0 (D:\RustPro\hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.36s
Running `target\debug\hello.exe hello`
the value is: 10
the value is: 20
the value is: 30
the value is: 40
the value is: 50
# 同时遍历集合下标和值
fn main() {
let arr = [10, 20, 30, 40, 50];
for (index, value) in arr.iter().enumerate() {
println!("Index: {}, Value: {}", index, value);
}
}
$cargo run hello
Compiling hello v0.1.0 (D:\RustPro\hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.39s
Running `target\debug\hello.exe hello`
Index: 0, Value: 10
Index: 1, Value: 20
Index: 2, Value: 30
Index: 3, Value: 40
Index: 4, Value: 50