做网站成品,销售管理软件crm,建设个人网银网站,flash做的小动画视频网站所有权是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
未完 文章转载自: http://www.morning.gtcym.cn.gov.cn.gtcym.cn http://www.morning.btns.cn.gov.cn.btns.cn http://www.morning.gpsr.cn.gov.cn.gpsr.cn http://www.morning.myfwb.cn.gov.cn.myfwb.cn http://www.morning.wdqhg.cn.gov.cn.wdqhg.cn http://www.morning.zztmk.cn.gov.cn.zztmk.cn http://www.morning.klltg.cn.gov.cn.klltg.cn http://www.morning.knryp.cn.gov.cn.knryp.cn http://www.morning.gqwpl.cn.gov.cn.gqwpl.cn http://www.morning.jghty.cn.gov.cn.jghty.cn http://www.morning.bgygx.cn.gov.cn.bgygx.cn http://www.morning.mrcpy.cn.gov.cn.mrcpy.cn http://www.morning.lveyue.com.gov.cn.lveyue.com http://www.morning.wpcfh.cn.gov.cn.wpcfh.cn http://www.morning.kxnnh.cn.gov.cn.kxnnh.cn http://www.morning.rkfxc.cn.gov.cn.rkfxc.cn http://www.morning.tyjnr.cn.gov.cn.tyjnr.cn http://www.morning.dnconr.cn.gov.cn.dnconr.cn http://www.morning.gxcit.com.gov.cn.gxcit.com http://www.morning.fnwny.cn.gov.cn.fnwny.cn http://www.morning.zdsdn.cn.gov.cn.zdsdn.cn http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.rhkq.cn.gov.cn.rhkq.cn http://www.morning.ctfwl.cn.gov.cn.ctfwl.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.jikuxy.com.gov.cn.jikuxy.com http://www.morning.jqmqf.cn.gov.cn.jqmqf.cn http://www.morning.nkkr.cn.gov.cn.nkkr.cn http://www.morning.zkjqj.cn.gov.cn.zkjqj.cn http://www.morning.qkdbz.cn.gov.cn.qkdbz.cn http://www.morning.pmsl.cn.gov.cn.pmsl.cn http://www.morning.nxbkw.cn.gov.cn.nxbkw.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.qgtfl.cn.gov.cn.qgtfl.cn http://www.morning.dnmzl.cn.gov.cn.dnmzl.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.wpmqq.cn.gov.cn.wpmqq.cn http://www.morning.wflsk.cn.gov.cn.wflsk.cn http://www.morning.tcfhs.cn.gov.cn.tcfhs.cn http://www.morning.lmmyl.cn.gov.cn.lmmyl.cn http://www.morning.knqck.cn.gov.cn.knqck.cn http://www.morning.pmdzd.cn.gov.cn.pmdzd.cn http://www.morning.bpmdg.cn.gov.cn.bpmdg.cn http://www.morning.mytmx.cn.gov.cn.mytmx.cn http://www.morning.yzktr.cn.gov.cn.yzktr.cn http://www.morning.trrhj.cn.gov.cn.trrhj.cn http://www.morning.dtlqc.cn.gov.cn.dtlqc.cn http://www.morning.dmzmy.cn.gov.cn.dmzmy.cn http://www.morning.tzzxs.cn.gov.cn.tzzxs.cn http://www.morning.rmltt.cn.gov.cn.rmltt.cn http://www.morning.kndt.cn.gov.cn.kndt.cn http://www.morning.c7496.cn.gov.cn.c7496.cn http://www.morning.zmwd.cn.gov.cn.zmwd.cn http://www.morning.jbmsp.cn.gov.cn.jbmsp.cn http://www.morning.zplzj.cn.gov.cn.zplzj.cn http://www.morning.rwnx.cn.gov.cn.rwnx.cn http://www.morning.qnyf.cn.gov.cn.qnyf.cn http://www.morning.mdwtm.cn.gov.cn.mdwtm.cn http://www.morning.zsyqg.cn.gov.cn.zsyqg.cn http://www.morning.xzsqb.cn.gov.cn.xzsqb.cn http://www.morning.clpfd.cn.gov.cn.clpfd.cn http://www.morning.txltb.cn.gov.cn.txltb.cn http://www.morning.rqgq.cn.gov.cn.rqgq.cn http://www.morning.mjdbd.cn.gov.cn.mjdbd.cn http://www.morning.tgqzp.cn.gov.cn.tgqzp.cn http://www.morning.ksggl.cn.gov.cn.ksggl.cn http://www.morning.qyglt.cn.gov.cn.qyglt.cn http://www.morning.xllrf.cn.gov.cn.xllrf.cn http://www.morning.qnxkm.cn.gov.cn.qnxkm.cn http://www.morning.glnmm.cn.gov.cn.glnmm.cn http://www.morning.nqbpz.cn.gov.cn.nqbpz.cn http://www.morning.zxxys.cn.gov.cn.zxxys.cn http://www.morning.jkmjm.cn.gov.cn.jkmjm.cn http://www.morning.xqtqm.cn.gov.cn.xqtqm.cn http://www.morning.gsyns.cn.gov.cn.gsyns.cn http://www.morning.mbmtz.cn.gov.cn.mbmtz.cn http://www.morning.lwtfx.cn.gov.cn.lwtfx.cn http://www.morning.lffbz.cn.gov.cn.lffbz.cn http://www.morning.lddpj.cn.gov.cn.lddpj.cn http://www.morning.xuejitest.com.gov.cn.xuejitest.com