网站标题更新,没学历可以学什么技术,西安网站关键词优化,搭建网站 优帮云所有权是rust的核心功能。
Rust 是通过所有权来管理内存回收的
栈#xff08;Stack#xff09; 栈是后进先出的 栈中存储的数据在编译时是固定内存大小的数据 数据移除栈叫出栈#xff0c;数据存入栈叫入栈 入栈要比在堆上分配内存块#xff0c;入栈时分配器无需为存储…所有权是rust的核心功能。
Rust 是通过所有权来管理内存回收的
栈Stack 栈是后进先出的 栈中存储的数据在编译时是固定内存大小的数据 数据移除栈叫出栈数据存入栈叫入栈 入栈要比在堆上分配内存块入栈时分配器无需为存储新数据去搜索内存空间其位置总是在顶部 在调用一个函数的时候传递给函数的值包括可能指向堆上数据的指针和函数的局部变量都会被压入栈中等函数结束时这些值被移除栈。
堆Heap 堆中存储的数据在编译时是的大小是未知的。 堆在存储数据的时候需要向分配器先申请内存空间并将空间标记为已使用状态并返回一个表示该位置地址的 指针pointer 访问堆上的数据比访问栈上的数据要慢因为必须通过指针来访问现代处理器在内存中跳转越少就越快。 移动
rust中变量与数据交互的方式是移动rust的移动和copy是不一样的copy 后被copy的变量是不会失效的但是移动当a 移动到b的时候a变量就会失效。
引用
引用reference像一个指针因为它是一个地址我们可以由此访问储存于该地址的属于其他变量的数据。 与指针不同引用确保指向某个特定类型的有效值。
与使用 引用相反的操作是 解引用dereferencing它使用解引用运算符*。我们将会在第八章遇到一些解引用运算符并在第十五章详细讨论解引用。
引用的规则
让我们概括一下之前对引用的讨论 在任意给定时间要么 只能有一个可变引用要么 只能有多个不可变引用。 引用必须总是有效的。
所有权 rust中每个值都有一个所有者。 值在任一时刻有且只有一个所有者。 当所有者变量离开作用域这个值就被丢弃。
let s hello;
{ // s 在这里无效它尚未声明let s hello; // 从此处起s 是有效的// 使用 s} // 此作用域已结束s 不再有效
值不能在同一个作用域里面即分配给可变变量又分配给不可变变量。
let mut s String::from(hello);
let r1 a;
let r2 mut a;//cannot borrow s as mutable because it is also borrowed as immutable
//mutable borrow occurs hererustcClick for full compiler diagnostic
//main.rs(207, 14): immutable borrow occurs here
//main.rs(209, 23): immutable borrow later used here
可变变量不能在同一作用域里面同时赋值给多个可变变量在同一作用域里面可以赋值给多个不可变变量但是会有编译警告
let mut a String::from(hello, world);
let r1 mut a;
let r2 mut a;
println!({},{}, r1, r2);//cannot borrow a as mutable more than once at a time
//second mutable borrow occurs hererustcClick for full compiler diagnostic
//main.rs(196, 14): first mutable borrow occurs here
//main.rs(198, 23): first borrow later used here 当一个不可变变量传入一个函数的时候我们是不能在函数里面进行修改的反之
fn main() {let s String::from(hello world);let word change(s);// s.clear(); // 错误println!(the first word is: {}, word);
}fn change(some_string: String) {some_string.push_str(, world);
}//cannot borrow *some_string as mutable, as it is behind a reference
//some_string is a reference, so the data it refers to cannot be borrowed as mutablerustcClick for full compiler diagnostic
//main.rs(243, 25): consider changing this to be a mutable reference: mut 在一个函数里面的变量的引用是不能被返回的这样会导致垂悬引用。
fn dangle() - String { // dangle 返回一个字符串的引用let s String::from(hello); // s 是一个新字符串s // 返回字符串 s 的引用
} // 这里 s 离开作用域并被丢弃。其内存被释放。// 危险 当一个可以变量传入一个函数但是该的参数是不可变变量那么后续要对该值进行修改是不被允许的。
fn main() {
let mut s String::from(hello world);let word first_word(s);s.clear(); // 错误println!(the first word is: {}, word);
}fn first_word(s: str) - str {let bytes s.as_bytes();for (i, item) in bytes.iter().enumerate() {if item b {return s[0..i];}}s[..]
}
//cannot borrow s as mutable because it is also borrowed as immutable
//mutable borrow occurs hererustcClick for full compiler diagnostic
//main.rs(201, 27): immutable borrow occurs here
//main.rs(205, 39): immutable borrow later used here
未完