Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use an immutable reference to i32

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?

like image 982
NXH Avatar asked Aug 15 '15 04:08

NXH


1 Answers

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.

like image 152
Vladimir Matveev Avatar answered Sep 21 '22 12:09

Vladimir Matveev