-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add get_opt to std::vec #9608
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
Add get_opt to std::vec #9608
Conversation
Ideally this method should not insert a boundary check for the vector indexing, so it would be implemented with unsafe code to achieve that (see I think this is something that vec should provide. Either directly or through a |
I think that Also, I don't think it should be implemented as an unsafe operation. |
@flaper87 " |
@huonw yeah, I knew that. I should stop writing things when I'm falling asleep. Ignore the FWIW, code looks good to me. |
I didn't suggest it should be unsafe, only implemented internally with unsafe code. Vector indexing like |
Here is the unsafe
|
@blake2-ppc I get what you mean, not quite sure why IR is the same for both, reducing the number of bound checks makes sense, nonetheless. |
I just had a look at the IR and it does indeed appear to be equivalent. As that's the case it are you fine with leaving it in it's current state, as it's simpler than the unsafe implementation? |
yes |
LGTM, @huonw r? |
This adds `get_opt` to `std::vec`, which looks up an item by index and returns an `Option`. If the given index is out of range, `None` will be returned, otherwise a `Some`-wrapped item will be returned. Example use case: ```rust use std::os; fn say_hello(name: &str) { println(fmt!("Hello, %s", name)); } fn main(){ // Try to get the first cmd line arg, but default to "World" let args = os::args(); let default = ~"World"; say_hello(*args.get_opt(1).unwrap_or(&default)); } ``` If there's an existing way of implementing this pattern that's cleaner, I'll happily close this. I'm also open to naming suggestions (`index_opt`?)
Don't suggest moving tuple structs with a significant drop to late evaluation fixes rust-lang#9608 changelog: Don't suggest moving tuple structs with a significant drop to late evaluation
This adds
get_opt
tostd::vec
, which looks up an item by index and returns anOption
. If the given index is out of range,None
will be returned, otherwise aSome
-wrapped item will be returned.Example use case:
If there's an existing way of implementing this pattern that's cleaner, I'll happily close this. I'm also open to naming suggestions (
index_opt
?)