@@ -6,6 +6,7 @@ use super::super::{
66} ;
77use crate :: array;
88use crate :: cmp:: { self , Ordering } ;
9+ use crate :: iter:: adapters:: { Dedup , DedupEq , DedupKey } ;
910use crate :: num:: NonZero ;
1011use crate :: ops:: { ChangeOutputType , ControlFlow , FromResidual , Residual , Try } ;
1112
@@ -1863,6 +1864,142 @@ pub trait Iterator {
18631864 Inspect :: new ( self , f)
18641865 }
18651866
1867+ /// Removes all but the first of consecutive repeated elements in the iterator
1868+ /// according to the [`PartialEq`] trait implementation.
1869+ ///
1870+ /// For an iterator yielding infinitely many consecutive duplicates,
1871+ /// calling [`next`][Iterator::next] on this iterator may never halt.
1872+ ///
1873+ /// If the iterator is sorted, this removes all duplicates.
1874+ ///
1875+ /// # Examples
1876+ ///
1877+ /// Basic usage:
1878+ ///
1879+ /// ```
1880+ /// #![feature(iter_dedup)]
1881+ ///
1882+ /// let vec = vec![1, 2, 2, 3, 2];
1883+ ///
1884+ /// let mut iter = vec.into_iter().dedup();
1885+ ///
1886+ /// assert_eq!(iter.next(), Some(1));
1887+ /// assert_eq!(iter.next(), Some(2));
1888+ /// assert_eq!(iter.next(), Some(3));
1889+ /// assert_eq!(iter.next(), Some(2));
1890+ /// assert_eq!(iter.next(), None);
1891+ /// ```
1892+ ///
1893+ /// Example of an infinite loop:
1894+ ///
1895+ /// ```no_run
1896+ /// #![feature(iter_dedup)]
1897+ ///
1898+ /// // this will never terminate
1899+ /// let _ = std::iter::repeat(2).dedup().next();
1900+ /// ```
1901+ #[ unstable( feature = "iter_dedup" , issue = "83747" ) ]
1902+ #[ inline]
1903+ fn dedup < F > ( self ) -> Dedup < Self , DedupEq >
1904+ where
1905+ Self : Sized ,
1906+ Self :: Item : PartialEq ,
1907+ {
1908+ Dedup :: new ( self , DedupEq )
1909+ }
1910+
1911+ /// Removes all but the first of consecutive elements in the iterator
1912+ /// satisfying a given equality relation.
1913+ ///
1914+ /// The `same_bucket` function is passed a references to two elements from
1915+ /// the iterator and must determine if the elements compare equal.
1916+ ///
1917+ /// For an iterator yielding infinitely many consecutive duplicates,
1918+ /// calling [`next`][Iterator::next] on this iterator may never halt.
1919+ ///
1920+ /// If the iterator is sorted, this removes all duplicates.
1921+ ///
1922+ /// # Examples
1923+ ///
1924+ /// Basic usage:
1925+ ///
1926+ /// ```
1927+ /// #![feature(iter_dedup)]
1928+ ///
1929+ /// let vec = vec!["foo", "bar", "Bar", "baz", "bar"];
1930+ ///
1931+ /// let mut iter = vec.into_iter().dedup_by(|a, b| a.eq_ignore_ascii_case(b));
1932+ ///
1933+ /// assert_eq!(iter.next(), Some("foo"));
1934+ /// assert_eq!(iter.next(), Some("bar"));
1935+ /// assert_eq!(iter.next(), Some("baz"));
1936+ /// assert_eq!(iter.next(), Some("bar"));
1937+ /// assert_eq!(iter.next(), None);
1938+ /// ```
1939+ ///
1940+ /// Example of an infinite loop:
1941+ ///
1942+ /// ```no_run
1943+ /// #![feature(iter_dedup)]
1944+ ///
1945+ /// // this will never terminate
1946+ /// let _ = std::iter::repeat(2).dedup_by(|a, b| a == b).next();
1947+ /// ```
1948+ #[ unstable( feature = "iter_dedup" , issue = "83747" ) ]
1949+ #[ inline]
1950+ fn dedup_by < F > ( self , f : F ) -> Dedup < Self , F >
1951+ where
1952+ Self : Sized ,
1953+ F : FnMut ( & Self :: Item , & Self :: Item ) -> bool ,
1954+ {
1955+ Dedup :: new ( self , f)
1956+ }
1957+
1958+ /// Removes all but the first of consecutive elements in the iterator
1959+ /// that resolve to the same key.
1960+ ///
1961+ /// For an iterator yielding infinitely many consecutive duplicates,
1962+ /// calling [`next`][Iterator::next] on this iterator may never halt.
1963+ ///
1964+ /// If the iterator is sorted, this removes all duplicates.
1965+ ///
1966+ /// # Examples
1967+ ///
1968+ /// Basic usage:
1969+ ///
1970+ /// ```
1971+ /// #![feature(iter_dedup)]
1972+ ///
1973+ /// let vec = vec![10, 20, 21, 30, 20];
1974+ ///
1975+ /// let mut iter = vec.into_iter().dedup_by_key(|&i| i / 10);
1976+ ///
1977+ /// assert_eq!(iter.next(), Some(10));
1978+ /// assert_eq!(iter.next(), Some(20));
1979+ /// assert_eq!(iter.next(), Some(30));
1980+ /// assert_eq!(iter.next(), Some(20));
1981+ /// assert_eq!(iter.next(), None);
1982+ /// ```
1983+ ///
1984+ /// Example of an infinite loop:
1985+ ///
1986+ /// ```no_run
1987+ /// #![feature(iter_dedup)]
1988+ ///
1989+ /// // this will never terminate
1990+ /// let _ = std::iter::repeat(2).dedup_by_key(|&n| n).next();
1991+ /// ```
1992+ #[ unstable( feature = "iter_dedup" , issue = "83747" ) ]
1993+ #[ inline]
1994+ fn dedup_by_key < F , K > ( self , f : F ) -> Dedup < Self , DedupKey < F > >
1995+ where
1996+ Self : Sized ,
1997+ F : FnMut ( & Self :: Item ) -> K ,
1998+ K : PartialEq ,
1999+ {
2000+ Dedup :: new ( self , DedupKey ( f) )
2001+ }
2002+
18662003 /// Creates a "by reference" adapter for this instance of `Iterator`.
18672004 ///
18682005 /// Consuming method calls (direct or indirect calls to `next`)
0 commit comments