-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tracking Issue for vec::push_with_ref #104075
Comments
What's the purpose of this method? |
The problem with theses kinds of methods in general is that the lifetime of the struct A(i32);
impl A {
fn incr_and_ref(&mut self) -> &i32 {
self.0 += 1;
&self.0
}
fn get(&self) -> &i32 {
&self.0
}
}
fn main() {
let a = A(0);
let a_ref = a.incr_and_ref();
let a_ref_2 = a.get(); // not allowed, even though in theory we only hold an immutable ref to the inner value of a
println!("{} {}", a_ref, a_ref_2)
} |
Yes I am coming to realize that. Is there any way around this issue? |
To my knowledge the only way to do this now is to push to the vec and then get the last element of the vec, which doesnt feel very thread safe to me, and also kinda just feels icky. This provides a more thread safe alternative. |
Also apologies for responding so late |
Rust is not like Go. Safe rust cannot have data races. |
No. See rust-lang/rfcs#3343 (comment) for an example of why, although this probably does not apply to Vec (that said, I am not in favor of this method) |
This API would be more useful if it returned a mutable reference. Prior art: Option::insert, Option::get_or_insert, Option::get_or_insert_with, as well as |
This doesn't seem ready. Closing this. |
Feature gate:
#![feature(vec_push_with_ref)]
This is a tracking issue for
Vec::push_with_ref
Similar to
Vec::push
, but returns a reference to the elements new spot in memory.Public API
Steps / History
Unresolved Questions
push_with_ref
)Footnotes
https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩
The text was updated successfully, but these errors were encountered: