新注册公司网站怎么做,工厂网站开发,河北省正定县城乡建设网站,wordpress翻页数字标题 一、概念二、规则三、示例3.1 变量作用域3.2 所有权的移交#xff08;深拷贝与浅拷贝#xff09;3.3 函数与所有权3.4 返回值与作用域3.5 引用的使用 四、切片(str) 一、概念
所有权是Rust的核心特性。所有程序在运行时都必须管理它们使用计算机内存的方式。Rust的… 标题 一、概念二、规则三、示例3.1 变量作用域3.2 所有权的移交深拷贝与浅拷贝3.3 函数与所有权3.4 返回值与作用域3.5 引用的使用 四、切片(str) 一、概念
所有权是Rust的核心特性。所有程序在运行时都必须管理它们使用计算机内存的方式。Rust的内存是通过一个所有权系统来管理的其中包含一组编译器在编译时检查的规则。在Rust中一个值是在栈还是堆上对语言的行为和为什么要做某些决定是有更大的影响的。Rust通过所有权系统管理内存编译器在编译时会根据一系列的规则进行检查。在运行时所有权系统的任何功能都不会减慢程序。
二、规则
Rust 中的每一个值都有一个被称为其所有者owner的变量值在任一时刻有且只有一个所有者当所有者变量离开作用域这个值将被丢弃函数参数的传递也会造成所有权的转移使用引用可以只使用变量而不转移所有权一个引用的作用域从声明的地方开始一直持续到最后一次使用为止
三、示例
3.1 变量作用域
下面的变量x超出了{}的作用域范围打印时报错cannot find value x in this scopehello变量自动在堆内存中申请了空间并且初始化为hello等出了作用域倒数第二个“}”号后自动调用drop函数释放内存。
fn main() {{let x 3;} println!(x {}, x); //cannot find value x in this scope{let hello String::from(hello);println!(hello {}, hello );}
}3.2 所有权的移交深拷贝与浅拷贝
在堆上申请的内存会在连续赋值的时候进行内存所有权的移交。可以使用clone函数进行堆内存的深拷贝
fn main() {let x 5;let y x; //栈内存没有任何影响let h String::from(HelloWorld!);let l h; //已经进行了所有权的移交h已不存在//let l h.clone(); //可以使用clon()函数重新申请空间//println!(h {},h); //error: value borrowed here after moveclone除外println!(l {},l);
}3.3 函数与所有权
将值传递给函数在语义上与给变量赋值相似。所有权转移的规则也相同
fn main() {let s String::from(Hello);take_ownership(s); //这里s发生了转移 let x 5;makes_copy(x); //栈上的变量x不受所有权影响println!({} {}, x, s); //s的所有权在take_ownership里因此这里无法打印
}fn take_ownership(src: String){println!({}, src);
}fn makes_copy(src: i32){println!({}, src)
}3.4 返回值与作用域
返回值可以把内存空间的所有权返回
fn main() {let s1 gives_ownership(); //来源于gives_ownership中的some_stringlet s2 String::from(hello); let s3 takes_and_gives_back(s2); //通过该函数所有权从s2转移到了s3println!({}{}{}, s1, s2, s3); //s2编译报错}fn gives_ownership() - String {let src String::from(hello); src //返回src的所有权
}fn takes_and_gives_back(a_string: String) - String{a_string
}3.5 引用的使用
在参数中使用引用就可以只传递变量而不传递所有权
fn main() {let mut s String::from(Hello);alter_string_value(mut s); //只传递s的值而不转移所有权println!({}, s); //s依然有效,输出“Hello,world”
}fn alter_string_value(src: mut String){ //可变引用src.push_str(,world);
}fn print_string_value(src: String){src.push_str(,world); //不可变引用不能修改
}在同一时间只能有一个对某一特定数据的可变引用尝试创建两个可变引用的代码将会失败。
fn main() {let mut s String::from(hello);let r1 mut s;let r2 mut s;println!({}, {}, r1, r2);
}报错信息如下 这个报错说这段代码是无效的我们不能在同一个作用域内多次将 s 作为可变变量。第一个可变的引用在 r1 中并且必须持续到在 println! 中使用它但是在那个可变引用的创建和它的使用之间我们又尝试在 r2 中创建另一个可变引用它引用了与 r1 相同的数据。
这样做是为了避免数据竞争数据竞争由三个行为造成
两个或更多指针同时访问同一数据。至少有一个指针被用来写入数据。没有同步数据访问的机制。 禁止同时使用可变与不可变引用 fn main() {let mut s String::from(hello);let r1 s; // 没问题let r2 s; // 没问题let r3 mut s; // 大问题println!({}, {}, and {}, r1, r2, r3);
}改成下面这样就行了依然是作用域的问题。
fn main() {let mut s String::from(hello);let r1 s; // 没问题let r2 s; // 没问题println!({} {}, r1, r2);let r3 mut s; // 没问题println!({}, r3);
}四、切片(str)
切片slice允许引用集合中一段连续的元素序列而不用引用整个集合字符串字面量就是切片因此它是不可变的可以采用字符串切片str作为参数类型因此这样就可以同时接收String和str类型的参数了定义函数时使用字符串切片代替字符串引用会使我们的API更加通用且不会损失任何功能
切片示例
fn main() {let s String::from(hello world!);let hello s[0..5]; //hello,取0到4字符, 也可以写成s[..5]let world s[5..]; //world取6到最后let whole s[..]; //整个字符串// s.clear();println!(*{}*,hello); //*hello*println!(*{}*,world); //* world!*println!({}, whole); //hello world!
}函数示例
fn main() {let s String::from(hello world!);let wordIndex first_world(s[..]); //使用完整的切片println!(wordIndex {}, wordIndex);let my_string_literal hello world;let wordIndex first_world(my_string_literal);println!(wordIndex {}, wordIndex);}//获得第一个单词
fn first_world(s: str) - str {let bytes s.as_bytes(); //转换为字节序for (index, item) in bytes.iter().enumerate(){if item b {return s[..index];}}s[..]
}
文章转载自: http://www.morning.zymgs.cn.gov.cn.zymgs.cn http://www.morning.pzrpz.cn.gov.cn.pzrpz.cn http://www.morning.ypfw.cn.gov.cn.ypfw.cn http://www.morning.ntlxg.cn.gov.cn.ntlxg.cn http://www.morning.jpkhn.cn.gov.cn.jpkhn.cn http://www.morning.bwgrd.cn.gov.cn.bwgrd.cn http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.pwrkl.cn.gov.cn.pwrkl.cn http://www.morning.ttcmdsg.cn.gov.cn.ttcmdsg.cn http://www.morning.gwjqq.cn.gov.cn.gwjqq.cn http://www.morning.zjrnq.cn.gov.cn.zjrnq.cn http://www.morning.rdpps.cn.gov.cn.rdpps.cn http://www.morning.xjtnp.cn.gov.cn.xjtnp.cn http://www.morning.rdpps.cn.gov.cn.rdpps.cn http://www.morning.jwwfk.cn.gov.cn.jwwfk.cn http://www.morning.mjqms.cn.gov.cn.mjqms.cn http://www.morning.ktrh.cn.gov.cn.ktrh.cn http://www.morning.wcqkp.cn.gov.cn.wcqkp.cn http://www.morning.nqyzg.cn.gov.cn.nqyzg.cn http://www.morning.fpryg.cn.gov.cn.fpryg.cn http://www.morning.bwjgb.cn.gov.cn.bwjgb.cn http://www.morning.kspfq.cn.gov.cn.kspfq.cn http://www.morning.pymff.cn.gov.cn.pymff.cn http://www.morning.tzpqc.cn.gov.cn.tzpqc.cn http://www.morning.wkmpx.cn.gov.cn.wkmpx.cn http://www.morning.hprmg.cn.gov.cn.hprmg.cn http://www.morning.fhqdb.cn.gov.cn.fhqdb.cn http://www.morning.tsxg.cn.gov.cn.tsxg.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.nrwr.cn.gov.cn.nrwr.cn http://www.morning.kmqlf.cn.gov.cn.kmqlf.cn http://www.morning.yzygj.cn.gov.cn.yzygj.cn http://www.morning.zczkm.cn.gov.cn.zczkm.cn http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.nrzbq.cn.gov.cn.nrzbq.cn http://www.morning.xphcg.cn.gov.cn.xphcg.cn http://www.morning.ynbyk.cn.gov.cn.ynbyk.cn http://www.morning.xrwbc.cn.gov.cn.xrwbc.cn http://www.morning.rbkgp.cn.gov.cn.rbkgp.cn http://www.morning.mumgou.com.gov.cn.mumgou.com http://www.morning.bpmnx.cn.gov.cn.bpmnx.cn http://www.morning.mqbdb.cn.gov.cn.mqbdb.cn http://www.morning.bkslb.cn.gov.cn.bkslb.cn http://www.morning.dnvhfh.cn.gov.cn.dnvhfh.cn http://www.morning.lfmwt.cn.gov.cn.lfmwt.cn http://www.morning.dnwlb.cn.gov.cn.dnwlb.cn http://www.morning.jcyyh.cn.gov.cn.jcyyh.cn http://www.morning.nytqy.cn.gov.cn.nytqy.cn http://www.morning.wrkhf.cn.gov.cn.wrkhf.cn http://www.morning.jllnh.cn.gov.cn.jllnh.cn http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.rxhn.cn.gov.cn.rxhn.cn http://www.morning.xnzmc.cn.gov.cn.xnzmc.cn http://www.morning.bhpsz.cn.gov.cn.bhpsz.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.xdjsx.cn.gov.cn.xdjsx.cn http://www.morning.rzdzb.cn.gov.cn.rzdzb.cn http://www.morning.xkhhy.cn.gov.cn.xkhhy.cn http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.rwmp.cn.gov.cn.rwmp.cn http://www.morning.hdtcj.cn.gov.cn.hdtcj.cn http://www.morning.rgnq.cn.gov.cn.rgnq.cn http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn http://www.morning.xqcbz.cn.gov.cn.xqcbz.cn http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.mzhhr.cn.gov.cn.mzhhr.cn http://www.morning.jkrrg.cn.gov.cn.jkrrg.cn http://www.morning.krwzy.cn.gov.cn.krwzy.cn http://www.morning.qwqzk.cn.gov.cn.qwqzk.cn http://www.morning.mnpdy.cn.gov.cn.mnpdy.cn http://www.morning.kzxlc.cn.gov.cn.kzxlc.cn http://www.morning.kfsfm.cn.gov.cn.kfsfm.cn http://www.morning.wdpbq.cn.gov.cn.wdpbq.cn http://www.morning.xckrj.cn.gov.cn.xckrj.cn http://www.morning.ynrzf.cn.gov.cn.ynrzf.cn http://www.morning.cfocyfa.cn.gov.cn.cfocyfa.cn http://www.morning.glncb.cn.gov.cn.glncb.cn http://www.morning.rfrx.cn.gov.cn.rfrx.cn