-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Option::flatten #60256
Option::flatten #60256
Conversation
r? @rkruppe (rust_highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I think it would be preferable to add this as a method Assigning over to random t-libs member; |
@Centril I agree, a flatten method would be a good idea. Do you think I should add both a flatten method and an |
Don't really have an opinion :) |
This comment has been minimized.
This comment has been minimized.
Option<Option<T>>
with Into
trait.
You probably could use unwrap_or_default instead of matching, couldn't you? |
This comment has been minimized.
This comment has been minimized.
That imposes a constraint What you can do instead is |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@ethanboxx once you're done with the changes, can you please squash them into a single commit? |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@Centril I dont understand this error. Can you explain why this is failing? |
@ethanboxx It seems to me you are getting a type inference error because you are giving the compiler too many options to pick from. Try removing the implementation and that should fix it. |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@Centril Whats the best way to solve this new issue. |
TL;DR: Add Documentation tests are external to the crate they're testing, so the In other places in the standard library, the |
Thanks for the PR @ethanboxx! I'm personally a bit wary on adding this method though since I feel like what it morally is striving to do is collapse any amount of inner options, but it doesn't handle, for example, The |
Note that this is the same for |
@alexcrichton You are correct. My original intention was for it flatten all nested layers, however, it quickly became apparent that it was not possible. I have added extra documentation to clarify this in a similar way |
squashed commit
Ok, seems reasonable to me! @bors: r+ |
📌 Commit fa7ba66 has been approved by |
Option::flatten This PR makes this possible. ```rust assert_eq!(Some(6), Some(Some(6)).flatten()); assert_eq!(Some(6), Some(Some(6)).into()); ```
@bors rollup |
Rollup of 9 pull requests Successful merges: - #59946 (Fix equivalent string in escape_default docs) - #60256 (Option::flatten) - #60305 (hir: remove LoweredNodeId) - #60334 (Stabilized vectored IO) - #60353 (Add test not to forget resolved ICE) - #60356 (Stabilize str::as_mut_ptr) - #60358 (Clarify the short explanation of E0207) - #60359 (resolve: Consider erroneous imports used to avoid duplicate diagnostics) - #60360 (Add test case for labeled break in const assignment) Failed merges: r? @ghost
Stabilize `Option::flatten` - PR: rust-lang#60256 - Tracking issue: rust-lang#60258 @elahn > I was trying to `flat_map()` and found `map().flatten()` does the trick. This has been on nightly for 4 months, can we stabilise it? @ethanboxx > @Centril Helped me get this merged. What is the stabilization process? @Centril > @ethanboxx I'd just file a PR to stabilize it and we'll ask T-libs to FCP. So here I am. I am was unsure what number to put in `since = "-"` so I copied what someone had done in a recent PR.
Stabilize `Option::flatten` - PR: rust-lang#60256 - Tracking issue: rust-lang#60258 @elahn > I was trying to `flat_map()` and found `map().flatten()` does the trick. This has been on nightly for 4 months, can we stabilise it? @ethanboxx > @Centril Helped me get this merged. What is the stabilization process? @Centril > @ethanboxx I'd just file a PR to stabilize it and we'll ask T-libs to FCP. So here I am. I am was unsure what number to put in `since = "-"` so I copied what someone had done in a recent PR.
Add Result<Result<T, E>, E>::flatten -> Result<T, E> This PR makes this possible (modulo type inference): ```rust assert_eq!(Ok(6), Ok(Ok(6)).flatten()); ``` Tracking issue: rust-lang#70142 <sub>largely cribbed directly from <https://github.com/rust-lang/rust/pull/60256></sub>
This PR makes this possible.