Why use an immutable reference to i32

99封情书 提交于 2019-12-23 08:39:27

问题


In the chapter Lifetimes of the Rust book, there's an example:

struct Foo<'a> {
    x: &'a i32,
}

fn main() {
    let y = &5; // this is the same as `let _y = 5; let y = &_y;`
    let f = Foo { x: y };

    println!("{}", f.x);
}

Why do they use x: &'a i32?

I think if it is just x: i32 then they cannot demonstrate the lifetime usage. However, is there any other reason behind it? Is there any production code that uses immutable reference to a primitive type like i32?


回答1:


In this particular case the reason is indeed to show the concept of lifetimes. As for the general case, however, I see no reason making an immutable reference to a primitive type (mutable references, of course, is another matter) except of when it is done in generic code:

struct Holder<'a, T> {
    r: &'a T
}

let x: i32 = 123;
let h: Holder<i32> = Holder { r: &x };

Here if you have such structure, you have no other choice as to use a reference to an i32. Naturally, this structure can also be used with other, non-primitive and non-movable types.

As Shepmaster has mentioned in comments, there is indeed a case where you have references to primitive types - it is by-reference iterators. Remember, by a convention (which the standard library follows) iter() method on a collection should return an iterator of references into the collection:

let v: Vec<i32> = vec![1, 2, 3, 4];
let i = v.iter();  // i is Iterator<Item=&i32>

Then almost all methods on the iterator which take a closure will accept closures whose argument is a reference:

i.map(|n| *n + 1)  // n is of type &i32

Note that this is in fact a consequence of the more general case with generics. Vectors and slices may contain arbitrary types, including non-moveable ones, so they just have to have methods which would allow their users to borrow their contents.



来源:https://stackoverflow.com/questions/32021676/why-use-an-immutable-reference-to-i32

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!