简体   繁体   English

为什么 Rust 编译器在它应该是不可变的地方接受可变值?

[英]Why does the Rust compiler accept a mutable value where it should've been immutable?

I have a struct called Layer and a save_as_ppm function inside a impl block, the signature of the function is this:我在 impl 块中有一个名为Layer的结构和一个save_as_ppm function , function 的签名是这样的:

fn save_as_ppm(&self, filename: &str){}

But other functions inside the impl block have &mut self parameter so when I create an instance I have to make it mutable (I don't know if it's called an instance in Rust).但是 impl 块中的其他函数有&mut self参数,所以当我创建一个实例时,我必须使其可变(我不知道它是否在 Rust 中称为实例)。

let mut inputs = Layer::new(SAMPLE_SIZE as usize, SAMPLE_SIZE as usize);

But when I call this save_as_ppm function:但是当我调用这个save_as_ppm function 时:

inputs.save_as_ppm(&filepath)

It compiles.它编译。 My question is why does it compile?我的问题是它为什么编译? save_as_ppm takes a reference to self but I've just passed a mutable self . save_as_ppm引用了self但我刚刚传递了一个可变的self Shouldn't the compiler give an error?编译器不应该给出错误吗? At least a warning?至少是一个警告?

save_as_ppm takes a reference to self but I've just passed a mutable self . save_as_ppm引用了self但我刚刚传递了一个可变的self

This is not what's happening here.这不是这里发生的事情。 A method call expression uses a resolution algorithm which enables users to call a method without having to explicitly create a suitable borrow of the receiving value. 方法调用表达式使用解析算法,该算法使用户能够调用方法,而无需显式创建合适的接收值借用。 If necessary, the borrow effectively made will match the requested receiving type .如有必要,有效借用将匹配请求的接收类型

In other words, the code换句话说,代码

inputs.save_as_ppm(&filepath)

is equivalent to this call in fully qualified syntax :等效于完全限定语法中的此调用:

Layer::save_as_ppm(&inputs, &filepath)

Note that an immutable borrow was made directly: there is no conversion between different kinds of borrows, and all of this is inconsequential to the mutability of the binding ( let mut inputs ).请注意,不可变借用是直接进行的:不同种类的借用之间没有转换,所有这些对于绑定的可变性( let mut inputs )都是无关紧要的。 Since both immutable and mutable variable bindings can be immutably borrowed, the code complies with the borrow checker and compiles.由于不可变和可变变量绑定都可以不可变地借用,因此代码符合借用检查器并编译。

As an addendum, nothing would stop you from really passing a mutable reference:作为附录,没有什么能阻止您真正传递可变引用:

Layer::save_as_ppm(&mut inputs, &filepath);

This works because the mutable reference is coerced into an immutable reference with the same lifetime.这是有效的,因为可变引用被强制转换为具有相同生命周期的不可变引用。 It is one of the enumerated type coercions available in Rust.它是 Rust 中可用的枚举类型强制转换之一。

See also:也可以看看:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rust 编译器不期望可变引用,而应该期望可变引用 - Rust compiler does not expect a mutable reference where a mutable reference should be expected 为什么在将变量移动到 scope 后 Rust 编译器错误“不能作为不可变借用,因为它也作为可变借用”? - Why does the Rust compiler error with "cannot borrow as immutable because it is also borrowed as mutable" after moving the variable into a scope? 为什么 Rust 允许对可变变量的不可变引用? - Why Does Rust Allow an Immutable Reference to a Mutable Variable? 为什么 Rust 编译器会在我用新值替换它时抱怨我使用了移动的值? - Why does the Rust compiler complain that I use a moved value when I've replaced it with a new value? 为什么使用Rust将可变结构传递给函数会导致字段不可变? - Why using Rust does passing a mutable struct to a function result in immutable fields? Rust编译器如何知道值是否已被移动? - How does the Rust compiler know whether a value has been moved or not? 为什么 Rust 不能对不可变变量进行可变借用? - Why can't Rust do mutable borrow on an immutable varaiable? 如果 Rust str 是不可变的,为什么我可以将 str 变量声明为可变的? - Why can I declare a str variable as mutable if Rust str is immutable? 为什么 Rust 编译器不优化代码,假设两个可变引用不能别名? - Why does the Rust compiler not optimize code assuming that two mutable references cannot alias? 为什么 Rust 不允许可变别名? - Why does Rust disallow mutable aliasing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM