当前位置:网站首页>每日一R「03」Borrow 语义与引用
每日一R「03」Borrow 语义与引用
2022-08-10 19:08:00 【InfoQ】
01-只读借用/引用
&x
*y
fn main() {
let data = vec![1, 2, 3, 4];
let data1 = data; // data 的所有权转移给了 data1,后续不能再使用 data
println!("sum of data1: {}", sum(data1)); // data1 的所有权转移到了函数 sum 中,后续不能再使用 data1
println!("data1: {:?}", data1); // error1
println!("sum of data: {}", sum(data)); // error2
}
fn sum(data: Vec<u32>) -> u32 {
data.iter().fold(0, |acc, x| acc + x)
}
fn main() {
let data = vec![1, 2, 3, 4];
let data1 = &data; // data1 为 data 的只读引用
println!("sum of data1: {}", sum(data1)); // 只读引用实现了 Copy trait
println!("data1: {:?}", data1); // 不会出错
println!("sum of data: {}", sum(&data));
}
fn sum(data: &Vec<u32>) -> u32 {
// 值的地址会改变么?引用的地址会改变么?
println!("addr of value: {:p}, addr of ref: {:p}", data, &data);
data.iter().fold(0, |acc, x| acc + x)
}
02-可变借用/引用
&mut x
- 在一个作用域内,仅允许一个活跃的可变引用。
- 在一个作用域内,活跃的可变引用(写)和只读引用(读)是互斥的,不能同时存在。
fn main() {
let mut x: u32 = 10;
let y = &mut x;
println!("{}", *y); // 1
let z = &mut x; // 2
*z = 30;
println!("{}", x);
}
边栏推荐
- The 2021 ICPC Asia Shanghai Regional Programming Contest D、E
- @Autowired annotation --required a single bean, but 2 were found causes and solutions
- 2022杭电多校七 Black Magic (签到)
- Redis 持久化机制
- 2022 Hangdian Multi-School Seven Black Magic (Sign-in)
- 子域名收集&Google搜索引擎语法
- YOLOv3 SPP源码分析
- 转铁蛋白Tf功能化β-榄香烯-雷公藤红素/紫杉醇PLGA纳米粒/雷公藤甲素脂质体(化学试剂)
- Heme - gold nanoparticles (Heme - AuNP) composite nanometer enzyme | gold nanoparticles nuclear porous hollow carbon nanometer spherical shell (Au @ HCNs) nano enzyme
- Iridium Ruthenium Alloy/Iridium Oxide Biomimetic Nanozyme | Palladium Nanozyme | GMP-Pd Nanozyme | Gold-Palladium Composite Nanozyme | Ternary Metal Pd-M-Ir Nanozyme |shell nanozyme
猜你喜欢
【知识分享】在音视频开发领域中SEI到底是个啥?
Redis persistence mechanism
你不知道的浏览器页面渲染机制
YOLOv3 SPP source analysis
Heme - gold nanoparticles (Heme - AuNP) composite nanometer enzyme | gold nanoparticles nuclear porous hollow carbon nanometer spherical shell (Au @ HCNs) nano enzyme
测试/开发程序员值这么多钱么?“我“不会愿赌服输......
从 GAN 到 WGAN
@Autowired annotation --required a single bean, but 2 were found causes and solutions
电脑如何去掉u盘写保护的状态
【C#】WCF和TCP消息通信练习,实现群聊功能
随机推荐
Colocate Join :ClickHouse的一种高性能分布式join查询模型
今日份bug,点击win10任务栏视窗动态壁纸消失的bug,暂未发现解决方法。
FEMRL: A Framework for Large-Scale Privacy-Preserving Linkage of Patients’ Electronic Health Rec论文总结
电脑为什么会蓝屏的原因
铁蛋白颗粒负载雷替曲塞/培美曲塞/磺胺地索辛/金刚烷(科研试剂)
转铁蛋白Tf功能化β-榄香烯-雷公藤红素/紫杉醇PLGA纳米粒/雷公藤甲素脂质体(化学试剂)
zip文件协议解析
回老家去?
mysql----group by、where以及聚合函数需要注意事项
Tf ferritin particles contain cisplatin / oxaliplatin / doxorubicin / methotrexate MTX / paclitaxel PTX and other drugs
What is the upstream bandwidth and downstream bandwidth of the server?
[Teach you how to do mini-games] How to lay out the hands of Dou Dizhu?See what the UP master of the 250,000 fan game area has to say
铁蛋白颗粒Tf包载多肽/凝集素/细胞色素C/超氧化物歧化酶/多柔比星(定制服务)
洛谷 P1629 邮递员送信 (三种最短路)
Hangdian Multi-School Seven 1003-Counting Stickmen (Combination Mathematics)
3D游戏建模学习路线
史上最全GIS相关软件(CAD、FME、Arcgis、ArcgisPro)
Keras deep learning combat (17) - image segmentation using U-Net architecture
Transferrin-modified osthole long-circulating liposomes/PEG-PLGA nanoparticles loaded with notoginsenoside R1 ([email prot
Linux服务器安装Redis,详细步骤。