-
Notifications
You must be signed in to change notification settings - Fork 24
Description
Proposal
Problem statement
Getting the only item out of a collection isn't hard, but can't be concisely written on like a single line.
While, conceptually, it's quite a simple operation.
At a high level, it represents the conversion of Collection<T> -> Option<T>
Motivating examples or use cases
I ran into this just now when writing some code in the compiler for attributes:
pub fn get_single_span(&self) -> Option<Span> {
let mut spans /*: Vec<Span>*/ = self.get_all_spans();
if spans.len() == 1 { return spans.pop() } else { None }
}or, written in terms of iterators (by @yoshuawuyts):
pub fn get_single_span(&self) -> Option<Span> {
let mut iter = self.get_all_spans().into_iter();
match iter.next() {
Some(item) => match iter.next() {
Some(_) => None,
None => Some(item),
None => None,
}
}There are various other ways to write the same pattern. If you're okay with references, matching against a 1-length slice for example
Solution sketch
it'd be much clearer in my opinion to have a method like this:
pub fn get_single_span(&self) -> Option<Span> {
self.get_all_spans().into_iter().only()
}We either implement this as calling .next() twice, returning None if the 2nd next call returns Some. Alternatively, we only implement this on ExactSizeIterator and measure its length first, or at least specialize the ExactSizeIterator case to do so.
Alternatives
I thought about adding this to individual collections first, Vec::single{_ref,_mut} or whatever.
After thinking about it some more however, that'd mean a lot of different methods, including possibly the ones for collections like HashMap which could get the only key/value/both. By making it part of Iterator, you can already express these variants, as well as {_ref_mut} cases using .iter(), .iter_mut() and .into_iter().
Finally, arbitrary iterable collections can enjoy this new api now, as opposed to only the ones we implement it for explicitly.
Another name for this operation could be single()
Links and related work
Discussed this with @WaffleLapkin who also liked the idea :3
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.