Proper way to get reference of something that’s just been pushed to a Vec

  Kiến thức lập trình

So I need a reference to something that’s just been pushed to a Vec, e.g.

my_vec.push(foo);
Somelib::do_something_with(&my_vec, &foo); // borrow checker says hi

Now, this works:

Somelib::do_something_with(&my_vec, &my_vec.last().unwrap());

But is that the way? Or what’s the proper way to do it?

1

LEAVE A COMMENT