Rust官方文档
针对集合Rust提供了两种复合数据类型,元组和数组。
元组(tuple)是一种复合数据类型,可以包含不同类型的元素,但一旦定义,元组长度不可变的。
# 标准定义
fn main() {
let tup: (i32, f64, u8, &str) = (500, 6.4, 1, "hello");
}
# 也可省略类型
fn main() {
let tup= (500, 6.4, 1, "hello");
}
Rust中使用元组数据有两种方法。
- 解构 即将一个元组拆成多个部分,每个部分赋值给一个变量
- 索引 即使用’.'配合索引进行使用
fn main() {
let tup: (i32, f64, u8, &str) = (500, 6.4, 1, "hello");
let (a,b,c,d) = tup;
println!("a={0},b={1},c={2},d={3}",a,b,c,d);
}
$cargo run hello
Compiling hello v0.1.0 (/home/kali/Desktop/ctf/script/RustPro/hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.34s
Running `target/debug/hello hello`
a=500,b=6.4,c=1,d=hello
fn main() {
let tup: (i32, f64, u8, &str) = (500, 6.4, 1, "hello");
println!("a={0},b={1},c={2},d={3}",tup.0,tup.1,tup.2,tup.3);
}
$argo run hello
Compiling hello v0.1.0 (/home/kali/Desktop/ctf/script/RustPro/hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.31s
Running `target/debug/hello hello`
a=500,b=6.4,c=1,d=hello
理论上元组是不能进行修改的,但是如果定义为可变元组,则可以修改元组内容,但修改值的类型必须和定义时一致。
特别注意:元组长度始终无法修改
fn main() {
let tup: (i32, f64, u8, &str) = (500, 6.4, 1, "hello");
println!("value:{}",tup.0);
tup.0 = 1000;
println!("After:{}",tup.0);
}
$cargo run hello
Compiling hello v0.1.0 (/home/kali/Desktop/ctf/script/RustPro/hello)
error[E0594]: cannot assign to `tup.0`, as `tup` is not declared as mutable
--> src/main.rs:4:5
|
4 | tup.0 = 1000;
| ^^^^^^^^^^^^ cannot assign
|
help: consider changing this to be mutable
|
2 | let mut tup: (i32, f64, u8, &str) = (500, 6.4, 1, "hello");
| +++
For more information about this error, try `rustc --explain E0594`.
error: could not compile `hello` (bin "hello") due to previous error
fn main() {
let mut tup: (i32, f64, u8, &str) = (500, 6.4, 1, "hello");
println!("value:{}",tup.0);
tup.0 = 1000;
println!("After:{}",tup.0);
}
$cargo run hello
Compiling hello v0.1.0 (/home/kali/Desktop/ctf/script/RustPro/hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.22s
Running `target/debug/hello hello`
value:500
After:1000
fn main() {
let mut tup: (i32, f64, u8, &str) = (500, 6.4, 1, "hello");
println!("value:{}",tup.0);
tup.0 = "what?";
println!("After:{}",tup.0);
}
$cargo run hello
Compiling hello v0.1.0 (/home/kali/Desktop/ctf/script/RustPro/hello)
error[E0308]: mismatched types
--> src/main.rs:4:13
|
4 | tup.0 = "what?";
| ----- ^^^^^^^ expected `i32`, found `&str`
| |
| expected due to the type of this binding
For more information about this error, try `rustc --explain E0308`.
error: could not compile `hello` (bin "hello") due to previous error
目前据我所知,Rust中可以称为数组的有可变数组、不可变数组和向量.
- 数组只能是单一数据类型的合集,比元组多了遍历的功能
- 向量与数组一样,是单一数据类型的合计,但是比数组强大太多,具有增、删、插入、排序等操作
# 标准定义方法
fn main() {
let a: [i32; 5] = [1, 2, 3, 4, 5];
}
=> a =[1, 2, 3, 4, 5]
# 简化类型定义方法
fn main() {
let a = [1, 2, 3, 4, 5];
}
=> a =[1, 2, 3, 4, 5]
# 相同初始值定义方法
fn main() {
let a = [3; 5];
}
=> a =[3, 3, 3, 3, 3]
定义方法与不可变数组一致,只是加了
mut
关键字
fn main() {
let mut v: [i32; 5] = [3;5];
}
=> a =[3, 3, 3, 3, 3]
可以利用数组名[下标]的方式对数组内容进行使用,数组内下标从0计算。
fn main() {
let array = [1, 2, 3, 4, 5];
println!("Second value:{:?}", array[1]);
}
$cargo run hello
Compiling hello v0.1.0 (/home/kali/Desktop/ctf/script/RustPro/hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.24s
Running `target/debug/hello hello`
Second value:2
fn main() {
let mut array = [1, 2, 3, 4, 5];
println!("value :{:?}", array);
array[2] = 100;
println!("After :{:?}", array);
}
$cargo run hello
Compiling hello v0.1.0 (/home/kali/Desktop/ctf/script/RustPro/hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.22s
Running `target/debug/hello hello`
value :[1, 2, 3, 4, 5]
After :[1, 2, 100, 4, 5]
# 值遍历
fn main() {
let array = [1, 2, 3, 4, 5];
for i in array{
println!("{}",i);
}
}
$cargo run hello
Compiling hello v0.1.0 (/home/kali/Desktop/ctf/script/RustPro/hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.24s
Running `target/debug/hello hello`
1
2
3
4
5
# 下标遍历
fn main() {
let array = [1, 2, 3, 4, 5];
for i in 0..5{
println!("{}",array[i]);
}
}
$cargo run hello
Compiling hello v0.1.0 (/home/kali/Desktop/ctf/script/RustPro/hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.21s
Running `target/debug/hello hello`
1
2
3
4
5
Rust中的向量(Vector)是一种动态数组,它可以存储相同类型的元素,并且其大小可以在运行时增长或缩小。
let mut v1 = Vec::new(); // 创建一个空的向量
let v2 = vec![1, 2, 3]; // 创建一个包含初始元素的向量
fn main() {
let v = vec![1, 2, 3];
for i in &v {
println!("{}", i);
}
}
fn main() {
let mut v = Vec::new();
v.push(1);
v.push(2);
v.push(3);
println!("{:?}",v);
}
$cargo run hello
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/hello hello`
[1, 2, 3]
fn main() {
let mut vec = vec![1, 2, 4, 5];
vec.insert(2, 3); // 在索引2的位置插入元素3
println!("{:?}", vec);
}
$cargo run hello
Compiling hello v0.1.0 (/home/kali/Desktop/ctf/script/RustPro/hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.26s
Running `target/debug/hello hello`
[1, 2, 3, 4, 5]
fn main() {
let mut vec = vec![1, 2, 3, 4, 5];
let removed_element = vec.remove(2); // 删除索引2处的元素
println!("Removed element: {}", removed_element); // 输出: Removed element: 3
println!("{:?}", vec); // 输出: [1, 2, 4, 5]
}
fn main() {
let mut vec = vec![1, 2, 3];
vec.pop();
println!("{:?}", vec); // 输出: [1, 2]
}
fn main() {
let mut vec = vec![1, 2, 3, 4, 5];
vec.truncate(3); // 缩减向量的大小为3
println!("{:?}", vec); // 输出: [1, 2, 3]
}
fn main() {
let mut vec = vec![1, 2, 3, 4, 5];
vec.clear(); // 删除所有元素
println!("{:?}", vec); // 输出: []
}
fn main() {
let mut vec = vec![1, 2, 3, 4, 5];
let drained: Vec<_> = vec.drain(1..3).collect(); // 删除索引1到2的元素(不包括3)并收集它们
println!("Drained elements: {:?}", drained); // 输出: Drained elements: [2, 3]
println!("{:?}", vec); // 输出: [1, 4, 5]
}
fn main() {
let mut numbers = vec![5, 2, 9, 1, 5, 6];
numbers.sort();
println!("{:?}", numbers); // 输出: [1, 2, 5, 5, 6, 9]
}
# 升序
fn main() {
let mut numbers = vec![5, 2, 9, 1, 5, 6];
numbers.sort_by(|a, b| a.cmp(b));
println!("{:?}", numbers); // 输出: [1, 2, 5, 5, 6, 9]
}
# 降序
fn main() {
let mut numbers = vec![5, 2, 9, 1, 5, 6];
numbers.sort_by(|a, b| b.cmp(a));
println!("{:?}", numbers); // 输出: [9, 6, 5, 5, 2, 1]
}
fn main() {
let mut numbers = vec![1, 2, 3, 4, 5];
numbers.reverse();
println!("{:?}", numbers); // 输出: [5, 4, 3, 2, 1]
}