"首次提交"

This commit is contained in:
2024-06-08 15:01:12 +08:00
parent 6e0f708d0a
commit 84349a2469
112 changed files with 3272 additions and 0 deletions

1
vector_learn/.gitignore vendored Executable file
View File

@ -0,0 +1 @@
/target

8
vector_learn/Cargo.toml Executable file
View File

@ -0,0 +1,8 @@
[package]
name = "vector_learn"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

36
vector_learn/src/main.rs Executable file
View File

@ -0,0 +1,36 @@
fn main() {
println!("Hello, world!");
vector_learn();
}
/* Vector的数据是存放在heap区域的。等到离开作用域后也会被释放掉。同样受到所有权系统的规则显示 */
fn vector_learn() {
//直接使用vec的关联函数new来构造一个新的空vec是会导致rust无法推断类型而报错
//let mut vec0 = Vec::new();
//当定义一个空vector后往里面添加元素后即可顺利推断类型就不会报错了。
let mut vec1 = Vec::new();
vec1.push(23);
//使用一个宏来初始化一个vector。
let mut vec0 = vec![1,2,3,4];
//建立了一个vec0的不可变借用
let vec1 = &vec0;
//往vec0里面添加了数据这样可能会导致vec的内存布局变化。继而导致vec1失效。所以不可以同时存在可变引用和不可变引用
//vec0.push(23);
//在这里使用了vec的不可变借用
print!("vec: [");
for it in vec1 {
print!("{},", it);
}
println!("]");
print!("vec + 50: [");
for it in &mut vec0 {
*it += 50;
print!("{},", it);
}
println!("]");
}