我正在浏览“ Rust Book”网站,以了解即将进行的工作面试的语言。在有关向量的一章中,有两个代码示例:
fn main() {
let v = vec![100, 32, 57];
for i in &v {
println!("{}", i);
}
}
而且:
fn main() {
let mut v = vec![100, 32, 57];
for i in &mut v {
*i += 50;
}
}
现在,我想知道,为什么对于第一个样本,当我们传达对矢量元素的引用 i in:
println!(“ {}”,i);
,
但在示例中,我们在向量的每个元素中添加50个50?
为什么/不能/不能执行以下操作:
fn main() {
let v = vec![100, 32, 57];
for i in &v {
println!("{}", *i); // why don't we have to dereference before we pass to println!?
}
}
或:
fn main() {
let mut v = vec![100, 32, 57];
for i in &mut v {
i += 50; // why can't we just add directly to the reference like this?
}
}
我必须误解了我阅读的内容,但是我认为Rust可以识别您需要何时需要解雇或不自动解雇。我想我不明白为什么我们需要在两个样本中取消(或不取消)。我提供的两个示例用我想知道的特定代码发表了评论。
I am going through the "Rust Book" website in order to learn the language for an upcoming job interview. In the chapter on vectors, there are two code samples:
fn main() {
let v = vec![100, 32, 57];
for i in &v {
println!("{}", i);
}
}
and:
fn main() {
let mut v = vec![100, 32, 57];
for i in &mut v {
*i += 50;
}
}
Now I am wondering, why is it that for the first sample, when we pass the reference to the vector element i into:
println!("{}", i);
but in the sample where we add 50 to each element of the vector, we have to dereference the element with * before we add to the 50?
Why don't/can't we do the following:
fn main() {
let v = vec![100, 32, 57];
for i in &v {
println!("{}", *i); // why don't we have to dereference before we pass to println!?
}
}
or:
fn main() {
let mut v = vec![100, 32, 57];
for i in &mut v {
i += 50; // why can't we just add directly to the reference like this?
}
}
I must have misunderstood what I read, but I thought Rust was able to discern when you need to dereference or not automatically. I guess I don't understand why we need to dereference (or not dereference) in the two samples. The two examples I provided are commented with the specific bits of code I am wondering about.
发布评论
评论(1)
我认为最简单的方法是第二个例子是“正常”。
i
是& mut i32
(仅i32
,因为没有什么可以推断出任何其他整数类型),因此要分配给它需要解释mut I32
。println!
示例是做一些“魔术”的人。println!
将格式化类型,无论是通过值还是通过参考传递。这非常方便,您不希望它(例如)克隆您要打印的每个字符串,然后在应用程序中以后使用。编辑:
为了完整,这种“魔术”根本不是真正的魔术,而是很好地利用语言功能。
println!
(就像其他所有确实格式化的标准宏一样,例如panic!
和格式!
)使用格式化机械来自标准库。这可以与任何实现 特征(或debug
特征如果您使用{:?}
)。和display
具有对于所有事物的参考“ > 带有参考。
I think the easiest way to look at this is that the second example is the "normal" one.
i
is a&mut i32
(onlyi32
because there's nothing from which to infer any other integer type), so to assign to it you need to dereference to amut i32
.The
println!
example is the one doing some "magic".println!
will format types whether they're passed by value or by reference. This is very handy, you wouldn't want it to (for example) clone every string you want to print out but then use later in the application.Edit:
For completeness, this "magic" isn't really magic at all, but nice use of language features.
println!
(like every other standard macro that does formatting, likepanic!
andformat!
) uses the formatting machinery from the standard library. This can work with any type that implements theDisplay
trait (or theDebug
trait if you use{:?}
). AndDisplay
has a blanket impl for all references of things the implementDisplay
(Debug
also does this):So anything you can format with a value, you can also format with a reference.