-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 as_slice and as_mut_slice to Option #92411
Conversation
Extracts an immutable or mutable slice from an option such that, if the option is a None value, the slice is empty, and if the option is a Some value, the slice is length one. The slice is, for all practical purposes except extracting a pointer, a reference to the actual contents of the owned option. If it's a Some value, slice index zero contains the contents of the owned option. If it's a None value, the slice is length zero and cannot be read from or written to.
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
What's the motivation here? |
@jhpratt A lot of functions will take a slice as an argument, and so I saw a question which involved having an option and needing to pass it to a function that took a slice. This is a tricky problem to solve because Solving it can lead down inefficient paths like It can lead down rabbitholes where you create a function that takes an option and tries to make a zero or one element array that you return a slice of, which won't compile, or something like The best solution given was a function much like this pull request (except with This pull request takes all the confusion out of it; can take an |
If |
It's not that it's the most common problem, but it's got to be somewhere near as common as the need for To try to raise the visibility of I disagree that options aren't semantically a slice. They're zero or one elements contiguous and so on in memory, and, as The only difference here is that the |
How something is laid out in memory has nothing to do with how it semantically is or is not a slice. Semantics is a high-level overview. Where are you seeing a mention of how a reference is semantically a slice of one element? That's not present in the documentation and it's an assertion I also disagree with. |
Unfortunately, I'm not sure what you mean by semantically a slice then. From what I've seen in Rust and other languages, a slice is specifically a finite sequence of elements of one type laid out contiguously in memory. Can you please clarify? |
As I said, it has nothing to do with how it's laid out in memory. Semantics deals with how you think about a value. I'd be quite surprised if a significant number of people think of an option as a slice. As an experienced programmer I know I never have; it's always mentally been the enum that it is. |
While I understand that some people don't currently think of it as sliceable, perhaps because it's an enum, this isn't the kind of surprise that leads to mistakes in coding, and there are plenty of unexpected ways of thinking about things in programming that people discover as they go along. If someone who understands what a slice is sees |
I actually would not understand what the method does without looking at its signature. My intuition from the name is that this would return Additions to the standard library aren't meant to be easy. Something not being prone to mistakes really isn't the threshold we're shooting for. Methods need to be at least relatively common, which I have seen any evidence of (and it's certainly not self-evident or I wouldn't be asking). |
Fair enough. I don't think there's a better name (a different one would diverge from the array and vector I'll look for how prevalent equivalent code is. |
Found that this was proposed before and rejected. See #27776. Will continue looking. |
Ping from triage: |
Added in #105871. |
Extracts an immutable or mutable slice from an option such that, if
the option is a None value, the slice is empty, and if the option
is a Some value, the slice is length one.
The slice is, for all practical purposes except extracting a
pointer, a reference to the actual contents of the owned option. If
it's a Some value, slice index zero contains the contents of the
owned option. If it's a None value, the slice is length zero and
cannot be read from or written to.