【发布时间】:2021-12-19 00:37:09
【问题描述】:
为什么write1 的参数不需要设置为 mut?它将值直接传递给另一个需要mut 访问权限的函数。
在write1 中需要mut 的任何理由似乎都适用于write2,但编译器不同意:
use std::io::Write;
fn main() {
let out = &mut std::io::stdout();
write1(out)
}
fn write1(mut out: impl Write) { // `mut` not required. Adding `mut` produces warning: "variable does not need to be mutable"
write2(out)
}
fn write2(mut out: impl Write) { // `mut` required! Otherwise error "cannot borrow `out` as mutable, as it is not declared as mutable"
writeln!(out, "hi").unwrap();
}
【问题讨论】:
标签: rust borrow-checker