Skip to content

Commit 645cb0c

Browse files
committed
Improve return type of NonEmpty::split
Fixes #60[^issue-60]. As pointed out in #60, splitting a `NonEmpty` that contains a single element can be ambiguous to a `NonEmpty` equivalent to `(x, [x])`. A better representation for this return type is `(&T, &[T], Option<&T>)`. [^issue-60]: #60 Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com> X-Clacks-Overhead: GNU Terry Pratchett
1 parent 8cf531a commit 645cb0c

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -516,17 +516,17 @@ impl<T> NonEmpty<T> {
516516
///
517517
/// // Guaranteed to have the last element and the elements
518518
/// // preceding it.
519-
/// assert_eq!(non_empty.split(), (&1, &[2, 3, 4][..], &5));
519+
/// assert_eq!(non_empty.split(), (&1, &[2, 3, 4][..], Some(&5)));
520520
///
521521
/// let non_empty = NonEmpty::new(1);
522522
///
523523
/// // Guaranteed to have the last element.
524-
/// assert_eq!(non_empty.split(), (&1, &[][..], &1));
524+
/// assert_eq!(non_empty.split(), (&1, &[][..], None));
525525
/// ```
526-
pub fn split(&self) -> (&T, &[T], &T) {
526+
pub fn split(&self) -> (&T, &[T], Option<&T>) {
527527
match self.tail.split_last() {
528-
None => (&self.head, &[], &self.head),
529-
Some((last, middle)) => (&self.head, middle, last),
528+
None => (&self.head, &[], None),
529+
Some((last, middle)) => (&self.head, middle, Some(last)),
530530
}
531531
}
532532

0 commit comments

Comments
 (0)