当前位置: 首页 > news >正文

网站备案费用公司网站建设流程

网站备案费用,公司网站建设流程,西安黄页,0基础怎么做网站模版什么是所有权 rust使用由编译器检查的一些规则构成的所有权系统来管理内存。且这不会影响程序的运行效率。 所有权规则 rust中每一个每一个值都有一个owner。在同一时刻,只能有一个owner。当这个owner超过范围,则该值会被丢弃。 String类型 为什么需…

什么是所有权

rust使用由编译器检查的一些规则构成的所有权系统来管理内存。且这不会影响程序的运行效率。

所有权规则

  • rust中每一个每一个值都有一个owner。
  • 在同一时刻,只能有一个owner。
  • 当这个owner超过范围,则该值会被丢弃。

String类型

为什么需要String

  • 已经有了字符串字面量了,为啥还需要String类型呢?字面量是硬编码在程序里面的,如果我们在编码的时候并不知道字符串是什么,那我们就不能用字符串字面量了。比如字符串来自用户输入,这时候就需要用String类型来存储了。
  • 更多字符串基本用法参考【Rust】字符串String类型学习

内存及分配

简单的情况

在String::from的地方,会申请内存;在}之前rust会调用drop方法释放s的内存。

{let s = String::from("hello"); // s is valid from this point forward// do stuff with s
}                                  // this scope is now over, and s is no// longer valid

字符串赋值的情况

  • 当将s1赋值给s2的时候,s1就会被废弃,字符串的堆内存就交给s2去管理了。如果赋值后,再使用s1就会出现编译错误。
  • rust语言不会自动进行任何的拷贝,所以可以认为自动拷贝都是高性能的。
  • let s2 = s1这个过程中相当于发生了move的动作,将字符串的所有权从s1移交给s2。
    let s1 = String::from("hello");let s2 = s1;

在这里插入图片描述

字符串克隆的情况

  • 既然直接赋值,会导致所有权转移。那么如何做到赋值时进行拷贝呢?这就需要用到clone方法。
let s1 = String::from("hello");
let s2 = s1.clone();
println!("s1 = {s1}, s2 = {s2}");
  • 因为clone方法会拷贝堆上的内存,所以其执行的代价是比较高的。

只在栈上的数据:拷贝

let x = 5;
let y = x;
println!("x = {x}, y = {y}");
  • 我们发现如果直接把整数类型x赋值给y,并且在使用x。编译并没有报错。
  • 这是因为整形是固定大小的类型,存放在栈上。拷贝的代价很低,其深拷贝和浅拷贝并没有特别大的区别。所以我们可以直接拷贝,而不需要move。

所有权和函数

  • 按照我们所有权的规则,如下代码就很容易理解。
fn main() {let s = String::from("hello");  // s comes into scopetakes_ownership(s);             // s's value moves into the function...// ... and so is no longer valid herelet x = 5;                      // x comes into scopemakes_copy(x);                  // x would move into the function,// but i32 is Copy, so it's okay to still// use x afterward} // Here, x goes out of scope, then s. But because s's value was moved, nothing// special happens.fn takes_ownership(some_string: String) { // some_string comes into scopeprintln!("{some_string}");
} // Here, some_string goes out of scope and `drop` is called. The backing// memory is freed.fn makes_copy(some_integer: i32) { // some_integer comes into scopeprintln!("{some_integer}");
} // Here, some_integer goes out of scope. Nothing special happens.

返回值和有效范围

fn main() {let s1 = gives_ownership();         // gives_ownership moves its return// value into s1let s2 = String::from("hello");     // s2 comes into scopelet s3 = takes_and_gives_back(s2);  // s2 is moved into// takes_and_gives_back, which also// moves its return value into s3
} // Here, s3 goes out of scope and is dropped. s2 was moved, so nothing// happens. s1 goes out of scope and is dropped.fn gives_ownership() -> String {             // gives_ownership will move its// return value into the function// that calls itlet some_string = String::from("yours"); // some_string comes into scopesome_string                              // some_string is returned and// moves out to the calling// function
}// This function takes a String and returns one
fn takes_and_gives_back(a_string: String) -> String { // a_string comes into// scopea_string  // a_string is returned and moves out to the calling function
}
http://www.hkea.cn/news/616285/

相关文章:

  • css怎么嵌入到html直通车关键词优化口诀
  • 虚拟网站php专业型seo网站关键词排名提升
  • 找人合伙做网站平台b2b电商平台
  • 手机网站建设技术方案找网站设计公司
  • 杭州如何设计公司网站惠州seo网站推广
  • 成都网站建设门户大连企业网站建站模板
  • 游戏卡充值可以做网站吗百度竞价关键词出价技巧
  • 建设企业网站就等于开展网络营销网店推广实训报告
  • dede无法更新网站主页到百度联盟注册
  • wordpress支持爱奇艺企业网站seo托管怎么做
  • 永川做网站的武汉百度快速排名提升
  • 做网站的故意给中病毒网络营销广告
  • 关于阅读类网站的建设规划书友情链接系统
  • 专业做幼儿园设计的网站百度小程序关键词优化
  • 腾龙时时彩做号网站整站优化关键词排名
  • 正规的网站制作与推广百度广告运营
  • 网站建设估价引擎搜索有哪些
  • 东莞网站建设选菲凡网络如何制作网站
  • 网站收录系统备案查询官网
  • 临朐县网站建设利用搜索引擎营销成功的案例
  • 利用网盘做视频网站镇江优化推广
  • 视频微网站开发哪个公司网站设计好
  • 品网站建设智能搜索引擎
  • 怎样在百度建网站seo建设者
  • 四海网络网站建设咨询什么叫做网络营销
  • 安徽建设网官方网站优化分析
  • 网站根目录文件名游戏推广员是做什么的
  • 个体工商户怎么做网站西安网站seo技术
  • 报名网站制作2345网址导航官网下载安装
  • 图书购物网站开发总结百度发广告需要多少钱