- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在浏览 the Rust book来自 rust 官方网站,遇到了 following paragraph :
Note that we needed to make
v1_iter
mutable: calling the next method on an iterator changes internal state that the iterator uses to keep track of where it is in the sequence. In other words, this code consumes, or uses up, the iterator. Each call to next eats up an item from the iterator. We didn’t need to makev1_iter
mutable when we used a for loop because the loop took ownership ofv1_iter
and made it mutable behind the scenes.
如果你注意到最后一行。它说 for 循环使可变变量在幕后不可变。如果可以的话,那么作为程序员的我们是否也可以这样做呢?
就像我知道这不安全,我们不应该那样做,但只是想知道这是否可能。
最佳答案
将不可变变量重新绑定(bind)到可变变量是完全没问题的,例如以下代码有效:
let x = 5;
let mut x = x;
在最后一条语句之后,我们可以改变 x
变量。其实想想,有两个变数,第一个是移到最新。函数也可以做同样的事情:
fn f(mut x: i32) {
x += 1;
}
let y = 5;
f(y);
Rust 中禁止的事情是将不可变引用更改为可变引用。这是一个重要的区别,因为与借来的值(value)相比,拥有的值(value)总是可以安全地改变。
关于for-loop - 通过获取所有权将不可变变量转换为可变变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62899029/
我正在使用 tcod-rs。用于绘制到 RootConsole 的每个方法都采用一个可变引用。中央循环是一个 while 循环,它等待窗口关闭、清除屏幕、绘制,然后刷新。 “检查窗口关闭”方法也采用可
我写了一个具有这种形式的函数: 结果 f(const IParameter& p); 我的意图是这个签名将明确表明函数没有取得参数 p 的所有权。 问题是 Result 将保留对 IParameter
这个问题在这里已经有了答案: 关闭 9 年前。 Possible Duplicate: What is a smart pointer and when should I use one? 我正在阅
假设我有一个类: class Scheduler { Scheduler(JobService *service); AddJob(JobID id, ISchedule *sched
我试图弄清楚所有权如何与函数 CVMetalTextureGetTexture 一起工作: CVMetalTextureRef textureRef; // ... textureRef is cre
这个问题在这里已经有了答案: Should we pass a shared_ptr by reference or by value? (10 个答案) 关闭 4 年前。 例如 class A {
我正在做一个附带项目,我需要根据他的 gmail 帐户或任何其他参数来验证 channel 是否属于某个用户……这基本上是为了避免假帐户。是否可以? 最佳答案 是的, 跟随 youtube 记录的链接
我在使用Core Foundation Array时发现了一个奇怪的问题!这是代码片段 fname = CFStringCreateWithFormat(kCFAllocatorDefault, NU
有没有一种方法可以设置在 apache 下运行的 php 来创建文件夹,该文件夹的文件夹属于创建它的程序的所有者,而不是由 apache 拥有? 使用 word press 它会创建要上传到的新文件夹
我编写了以下函数来使用 boost.date_time 获取日期/时间字符串. namespace bpt = boost::posix_time; string get_date_time_stri
我在使用 Docker 容器时遇到了一个有点烦人的问题(我在 Ubuntu 上,所以没有像 VMWare 或 b2d 这样的虚拟化)。我已经构建了我的镜像,并且有一个正在运行的容器,它有一个来 sel
根据大多数示例,逻辑上最少有 3 个组织 ( org1, org2, orderer )。 实际上只有 2 个物理组织 ( org1, org2 )。任一组织或约定的第 3 方必须移交订购者组织的职责
我开始学习 Rust,在进行实验时,我发现所有权如何应用于我不理解的元组和数组的方式有所不同。基本上,以下代码显示了差异: #![allow(unused_variables)] struct Inn
我们有一个应用程序,其表单上有许多组件(面板、选项卡、编辑、组合框等)。但根据用户配置文件,其中大多数可以自动填充和/或不可见。因此,用户可以更快地完成工作。 问题:是否有更简单的方法可以在运行时创建
我有以下代码片段: fn f u32>(c: T) { println!("Hello {}", c()); } fn main() { let mut x = 32; let
我想执行示例中的代码: require_once 'google-api-php-client/vendor/autoload.php'; $client = new Google_C
这个问题在这里已经有了答案: What is move semantics? (11 个答案) 关闭 3 年前。 我有一个看起来像这样的构造函数: Thing::Thing(std::vector
我们正在使用服务帐户从服务器上传文件,但它已达到其存储配额限制。所有文件都已添加到另一个用户(具有 100 Gb 存储配额的 @gmail.com 帐户)创建的文件夹下,但上传的所有文件均归该服务帐户
我正处于 this question 中描述的 sme 情况。 .那个提问者找到的解决方案是 Full access !== Owner. I need to read the documentati
我正处于 this question 中描述的 sme 情况。 .那个提问者找到的解决方案是 Full access !== Owner. I need to read the documentati
我是一名优秀的程序员,十分优秀!