学习Rust的第7天:参考资料

Hey Everyone, 大家好,

Today is references and borrowing. Immutable references allow reading data without ownership transfer, while mutable references enable modification, subject to rules ensuring exclusive access and preventing data races.
今天的内容是引用和借用。这是内存安全的基本概念。不可变引用允许阅读数据而不需要所有权转移,而可变引用允许修改,但要遵守确保独占访问和防止数据竞争的规则。

Referencing 引用

Yesterday we saw how passing an argument to a function does the same effect as assigning the value to another variable and effectively resulting in a loss of ownership. Well there’s a workaround to that.
昨天我们看到了如何将参数传递给一个函数与将值分配给另一个变量产生相同的效果,并有效地导致所有权的丧失。有一个解决办法。
If you know anything about C, pointers are an essential concept of C. Well references are similar.
如果你对C有所了解,那么 pointers 是C的一个基本概念。参考文献是相似的。

Lets take a look at the code and understand it:
让我们看看代码并理解它:

fn main(){
  let s1: String = String::from("Hello World");
  let length: usize = calculate_length(&s1); // & is used to pass in a reference to a string
  println!("The length of `{}` is {}" ,s1,length);
}

fn calculate_length(s: &String) -> usize {
  s.len()
 }

Rust references are pointers t

  • 16
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老父亲的能量嘎嘣脆

感谢支持,共同成长

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值