1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 //! Type `Option` represents an optional value: every `Option`
14 //! is either `Some` and contains a value, or `None`, and
15 //! does not. `Option` types are very common in Rust code, as
16 //! they have a number of uses:
19 //! * Return values for functions that are not defined
20 //! over their entire input range (partial functions)
21 //! * Return value for otherwise reporting simple errors, where `None` is
23 //! * Optional struct fields
24 //! * Struct fields that can be loaned or "taken"
25 //! * Optional function arguments
26 //! * Nullable pointers
27 //! * Swapping things out of difficult situations
29 //! Options are commonly paired with pattern matching to query the presence
30 //! of a value and take action, always accounting for the `None` case.
33 //! fn divide(numerator: f64, denominator: f64) -> Option<f64> {
34 //! if denominator == 0.0 {
37 //! Some(numerator / denominator)
41 //! // The return value of the function is an option
42 //! let result = divide(2.0, 3.0);
44 //! // Pattern match to retrieve the value
46 //! // The division was valid
47 //! Some(x) => println!("Result: {}", x),
48 //! // The division was invalid
49 //! None => println!("Cannot divide by 0"),
54 // FIXME: Show how `Option` is used in practice, with lots of methods
56 //! # Options and pointers ("nullable" pointers)
58 //! Rust's pointer types must always point to a valid location; there are
59 //! no "null" pointers. Instead, Rust has *optional* pointers, like
60 //! the optional owned box, `Option<Box<T>>`.
62 //! The following example uses `Option` to create an optional box of
63 //! `i32`. Notice that in order to use the inner `i32` value first the
64 //! `check_optional` function needs to use pattern matching to
65 //! determine whether the box has a value (i.e. it is `Some(...)`) or
69 //! let optional: Option<Box<i32>> = None;
70 //! check_optional(&optional);
72 //! let optional: Option<Box<i32>> = Some(Box::new(9000));
73 //! check_optional(&optional);
75 //! fn check_optional(optional: &Option<Box<i32>>) {
77 //! Some(ref p) => println!("have value {}", p),
78 //! None => println!("have no value"),
83 //! This usage of `Option` to create safe nullable pointers is so
84 //! common that Rust does special optimizations to make the
85 //! representation of `Option<Box<T>>` a single pointer. Optional pointers
86 //! in Rust are stored as efficiently as any other pointer type.
90 //! Basic pattern matching on `Option`:
93 //! let msg = Some("howdy");
95 //! // Take a reference to the contained string
96 //! if let Some(ref m) = msg {
97 //! println!("{}", *m);
100 //! // Remove the contained string, destroying the Option
101 //! let unwrapped_msg = msg.unwrap_or("default message");
104 //! Initialize a result to `None` before a loop:
107 //! enum Kingdom { Plant(u32, &'static str), Animal(u32, &'static str) }
109 //! // A list of data to search through.
110 //! let all_the_big_things = [
111 //! Kingdom::Plant(250, "redwood"),
112 //! Kingdom::Plant(230, "noble fir"),
113 //! Kingdom::Plant(229, "sugar pine"),
114 //! Kingdom::Animal(25, "blue whale"),
115 //! Kingdom::Animal(19, "fin whale"),
116 //! Kingdom::Animal(15, "north pacific right whale"),
119 //! // We're going to search for the name of the biggest animal,
120 //! // but to start with we've just got `None`.
121 //! let mut name_of_biggest_animal = None;
122 //! let mut size_of_biggest_animal = 0;
123 //! for big_thing in &all_the_big_things {
124 //! match *big_thing {
125 //! Kingdom::Animal(size, name) if size > size_of_biggest_animal => {
126 //! // Now we've found the name of some big animal
127 //! size_of_biggest_animal = size;
128 //! name_of_biggest_animal = Some(name);
130 //! Kingdom::Animal(..) | Kingdom::Plant(..) => ()
134 //! match name_of_biggest_animal {
135 //! Some(name) => println!("the biggest animal is {}", name),
136 //! None => println!("there are no animals :("),
140 #![stable(feature = "rust1", since = "1.0.0")]
146 use default::Default
;
147 use iter
::ExactSizeIterator
;
148 use iter
::{Iterator, DoubleEndedIterator, FromIterator, IntoIterator}
;
151 use result
::Result
::{Ok, Err}
;
154 // Note that this is not a lang item per se, but it has a hidden dependency on
155 // `Iterator`, which is one. The compiler assumes that the `next` method of
156 // `Iterator` is an enumeration with one type parameter and two variants,
157 // which basically means it must be `Option`.
159 /// The `Option` type. See [the module level documentation](index.html) for more.
160 #[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
161 #[stable(feature = "rust1", since = "1.0.0")]
164 #[stable(feature = "rust1", since = "1.0.0")]
167 #[stable(feature = "rust1", since = "1.0.0")]
168 Some(#[stable(feature = "rust1", since = "1.0.0")] T)
171 /////////////////////////////////////////////////////////////////////////////
172 // Type implementation
173 /////////////////////////////////////////////////////////////////////////////
176 /////////////////////////////////////////////////////////////////////////
177 // Querying the contained values
178 /////////////////////////////////////////////////////////////////////////
180 /// Returns `true` if the option is a `Some` value
185 /// let x: Option<u32> = Some(2);
186 /// assert_eq!(x.is_some(), true);
188 /// let x: Option<u32> = None;
189 /// assert_eq!(x.is_some(), false);
192 #[stable(feature = "rust1", since = "1.0.0")]
193 pub fn is_some(&self) -> bool
{
200 /// Returns `true` if the option is a `None` value
205 /// let x: Option<u32> = Some(2);
206 /// assert_eq!(x.is_none(), false);
208 /// let x: Option<u32> = None;
209 /// assert_eq!(x.is_none(), true);
212 #[stable(feature = "rust1", since = "1.0.0")]
213 pub fn is_none(&self) -> bool
{
217 /////////////////////////////////////////////////////////////////////////
218 // Adapter for working with references
219 /////////////////////////////////////////////////////////////////////////
221 /// Converts from `Option<T>` to `Option<&T>`
225 /// Convert an `Option<String>` into an `Option<usize>`, preserving the original.
226 /// The `map` method takes the `self` argument by value, consuming the original,
227 /// so this technique uses `as_ref` to first take an `Option` to a reference
228 /// to the value inside the original.
231 /// let num_as_str: Option<String> = Some("10".to_string());
232 /// // First, cast `Option<String>` to `Option<&String>` with `as_ref`,
233 /// // then consume *that* with `map`, leaving `num_as_str` on the stack.
234 /// let num_as_int: Option<usize> = num_as_str.as_ref().map(|n| n.len());
235 /// println!("still can print num_as_str: {:?}", num_as_str);
238 #[stable(feature = "rust1", since = "1.0.0")]
239 pub fn as_ref(&self) -> Option
<&T
> {
241 Some(ref x
) => Some(x
),
246 /// Converts from `Option<T>` to `Option<&mut T>`
251 /// let mut x = Some(2);
252 /// match x.as_mut() {
253 /// Some(v) => *v = 42,
256 /// assert_eq!(x, Some(42));
259 #[stable(feature = "rust1", since = "1.0.0")]
260 pub fn as_mut(&mut self) -> Option
<&mut T
> {
262 Some(ref mut x
) => Some(x
),
267 /////////////////////////////////////////////////////////////////////////
268 // Getting to contained values
269 /////////////////////////////////////////////////////////////////////////
271 /// Unwraps an option, yielding the content of a `Some`.
275 /// Panics if the value is a `None` with a custom panic message provided by
281 /// let x = Some("value");
282 /// assert_eq!(x.expect("the world is ending"), "value");
285 /// ```{.should_panic}
286 /// let x: Option<&str> = None;
287 /// x.expect("the world is ending"); // panics with `the world is ending`
290 #[stable(feature = "rust1", since = "1.0.0")]
291 pub fn expect(self, msg
: &str) -> T
{
294 None
=> expect_failed(msg
),
298 /// Moves the value `v` out of the `Option<T>` if it is `Some(v)`.
302 /// Panics if the self value equals `None`.
306 /// In general, because this function may panic, its use is discouraged.
307 /// Instead, prefer to use pattern matching and handle the `None`
313 /// let x = Some("air");
314 /// assert_eq!(x.unwrap(), "air");
317 /// ```{.should_panic}
318 /// let x: Option<&str> = None;
319 /// assert_eq!(x.unwrap(), "air"); // fails
322 #[stable(feature = "rust1", since = "1.0.0")]
323 pub fn unwrap(self) -> T
{
326 None
=> panic
!("called `Option::unwrap()` on a `None` value"),
330 /// Returns the contained value or a default.
335 /// assert_eq!(Some("car").unwrap_or("bike"), "car");
336 /// assert_eq!(None.unwrap_or("bike"), "bike");
339 #[stable(feature = "rust1", since = "1.0.0")]
340 pub fn unwrap_or(self, def
: T
) -> T
{
347 /// Returns the contained value or computes it from a closure.
353 /// assert_eq!(Some(4).unwrap_or_else(|| 2 * k), 4);
354 /// assert_eq!(None.unwrap_or_else(|| 2 * k), 20);
357 #[stable(feature = "rust1", since = "1.0.0")]
358 pub fn unwrap_or_else
<F
: FnOnce() -> T
>(self, f
: F
) -> T
{
365 /////////////////////////////////////////////////////////////////////////
366 // Transforming contained values
367 /////////////////////////////////////////////////////////////////////////
369 /// Maps an `Option<T>` to `Option<U>` by applying a function to a contained value
373 /// Convert an `Option<String>` into an `Option<usize>`, consuming the original:
376 /// let maybe_some_string = Some(String::from("Hello, World!"));
377 /// // `Option::map` takes self *by value*, consuming `maybe_some_string`
378 /// let maybe_some_len = maybe_some_string.map(|s| s.len());
380 /// assert_eq!(maybe_some_len, Some(13));
383 #[stable(feature = "rust1", since = "1.0.0")]
384 pub fn map
<U
, F
: FnOnce(T
) -> U
>(self, f
: F
) -> Option
<U
> {
386 Some(x
) => Some(f(x
)),
391 /// Applies a function to the contained value (if any),
392 /// or returns a `default` (if not).
397 /// let x = Some("foo");
398 /// assert_eq!(x.map_or(42, |v| v.len()), 3);
400 /// let x: Option<&str> = None;
401 /// assert_eq!(x.map_or(42, |v| v.len()), 42);
404 #[stable(feature = "rust1", since = "1.0.0")]
405 pub fn map_or
<U
, F
: FnOnce(T
) -> U
>(self, default: U
, f
: F
) -> U
{
412 /// Applies a function to the contained value (if any),
413 /// or computes a `default` (if not).
420 /// let x = Some("foo");
421 /// assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 3);
423 /// let x: Option<&str> = None;
424 /// assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 42);
427 #[stable(feature = "rust1", since = "1.0.0")]
428 pub fn map_or_else
<U
, D
: FnOnce() -> U
, F
: FnOnce(T
) -> U
>(self, default: D
, f
: F
) -> U
{
435 /// Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to
436 /// `Ok(v)` and `None` to `Err(err)`.
441 /// let x = Some("foo");
442 /// assert_eq!(x.ok_or(0), Ok("foo"));
444 /// let x: Option<&str> = None;
445 /// assert_eq!(x.ok_or(0), Err(0));
448 #[stable(feature = "rust1", since = "1.0.0")]
449 pub fn ok_or
<E
>(self, err
: E
) -> Result
<T
, E
> {
456 /// Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to
457 /// `Ok(v)` and `None` to `Err(err())`.
462 /// let x = Some("foo");
463 /// assert_eq!(x.ok_or_else(|| 0), Ok("foo"));
465 /// let x: Option<&str> = None;
466 /// assert_eq!(x.ok_or_else(|| 0), Err(0));
469 #[stable(feature = "rust1", since = "1.0.0")]
470 pub fn ok_or_else
<E
, F
: FnOnce() -> E
>(self, err
: F
) -> Result
<T
, E
> {
477 /////////////////////////////////////////////////////////////////////////
478 // Iterator constructors
479 /////////////////////////////////////////////////////////////////////////
481 /// Returns an iterator over the possibly contained value.
487 /// assert_eq!(x.iter().next(), Some(&4));
489 /// let x: Option<u32> = None;
490 /// assert_eq!(x.iter().next(), None);
493 #[stable(feature = "rust1", since = "1.0.0")]
494 pub fn iter(&self) -> Iter
<T
> {
495 Iter { inner: Item { opt: self.as_ref() }
}
498 /// Returns a mutable iterator over the possibly contained value.
503 /// let mut x = Some(4);
504 /// match x.iter_mut().next() {
505 /// Some(v) => *v = 42,
508 /// assert_eq!(x, Some(42));
510 /// let mut x: Option<u32> = None;
511 /// assert_eq!(x.iter_mut().next(), None);
514 #[stable(feature = "rust1", since = "1.0.0")]
515 pub fn iter_mut(&mut self) -> IterMut
<T
> {
516 IterMut { inner: Item { opt: self.as_mut() }
}
519 /////////////////////////////////////////////////////////////////////////
520 // Boolean operations on the values, eager and lazy
521 /////////////////////////////////////////////////////////////////////////
523 /// Returns `None` if the option is `None`, otherwise returns `optb`.
529 /// let y: Option<&str> = None;
530 /// assert_eq!(x.and(y), None);
532 /// let x: Option<u32> = None;
533 /// let y = Some("foo");
534 /// assert_eq!(x.and(y), None);
537 /// let y = Some("foo");
538 /// assert_eq!(x.and(y), Some("foo"));
540 /// let x: Option<u32> = None;
541 /// let y: Option<&str> = None;
542 /// assert_eq!(x.and(y), None);
545 #[stable(feature = "rust1", since = "1.0.0")]
546 pub fn and
<U
>(self, optb
: Option
<U
>) -> Option
<U
> {
553 /// Returns `None` if the option is `None`, otherwise calls `f` with the
554 /// wrapped value and returns the result.
556 /// Some languages call this operation flatmap.
561 /// fn sq(x: u32) -> Option<u32> { Some(x * x) }
562 /// fn nope(_: u32) -> Option<u32> { None }
564 /// assert_eq!(Some(2).and_then(sq).and_then(sq), Some(16));
565 /// assert_eq!(Some(2).and_then(sq).and_then(nope), None);
566 /// assert_eq!(Some(2).and_then(nope).and_then(sq), None);
567 /// assert_eq!(None.and_then(sq).and_then(sq), None);
570 #[stable(feature = "rust1", since = "1.0.0")]
571 pub fn and_then
<U
, F
: FnOnce(T
) -> Option
<U
>>(self, f
: F
) -> Option
<U
> {
578 /// Returns the option if it contains a value, otherwise returns `optb`.
585 /// assert_eq!(x.or(y), Some(2));
588 /// let y = Some(100);
589 /// assert_eq!(x.or(y), Some(100));
592 /// let y = Some(100);
593 /// assert_eq!(x.or(y), Some(2));
595 /// let x: Option<u32> = None;
597 /// assert_eq!(x.or(y), None);
600 #[stable(feature = "rust1", since = "1.0.0")]
601 pub fn or(self, optb
: Option
<T
>) -> Option
<T
> {
608 /// Returns the option if it contains a value, otherwise calls `f` and
609 /// returns the result.
614 /// fn nobody() -> Option<&'static str> { None }
615 /// fn vikings() -> Option<&'static str> { Some("vikings") }
617 /// assert_eq!(Some("barbarians").or_else(vikings), Some("barbarians"));
618 /// assert_eq!(None.or_else(vikings), Some("vikings"));
619 /// assert_eq!(None.or_else(nobody), None);
622 #[stable(feature = "rust1", since = "1.0.0")]
623 pub fn or_else
<F
: FnOnce() -> Option
<T
>>(self, f
: F
) -> Option
<T
> {
630 /////////////////////////////////////////////////////////////////////////
632 /////////////////////////////////////////////////////////////////////////
634 /// Takes the value out of the option, leaving a `None` in its place.
639 /// let mut x = Some(2);
641 /// assert_eq!(x, None);
643 /// let mut x: Option<u32> = None;
645 /// assert_eq!(x, None);
648 #[stable(feature = "rust1", since = "1.0.0")]
649 pub fn take(&mut self) -> Option
<T
> {
650 mem
::replace(self, None
)
654 impl<'a
, T
: Clone
> Option
<&'a T
> {
655 /// Maps an `Option<&T>` to an `Option<T>` by cloning the contents of the
657 #[stable(feature = "rust1", since = "1.0.0")]
658 pub fn cloned(self) -> Option
<T
> {
659 self.map(|t
| t
.clone())
663 impl<T
: Default
> Option
<T
> {
664 /// Returns the contained value or a default
666 /// Consumes the `self` argument then, if `Some`, returns the contained
667 /// value, otherwise if `None`, returns the default value for that
672 /// Convert a string to an integer, turning poorly-formed strings
673 /// into 0 (the default value for integers). `parse` converts
674 /// a string to any other type that implements `FromStr`, returning
678 /// let good_year_from_input = "1909";
679 /// let bad_year_from_input = "190blarg";
680 /// let good_year = good_year_from_input.parse().ok().unwrap_or_default();
681 /// let bad_year = bad_year_from_input.parse().ok().unwrap_or_default();
683 /// assert_eq!(1909, good_year);
684 /// assert_eq!(0, bad_year);
687 #[stable(feature = "rust1", since = "1.0.0")]
688 pub fn unwrap_or_default(self) -> T
{
691 None
=> Default
::default(),
696 // This is a separate function to reduce the code size of .expect() itself.
699 fn expect_failed(msg
: &str) -> ! {
704 /////////////////////////////////////////////////////////////////////////////
705 // Trait implementations
706 /////////////////////////////////////////////////////////////////////////////
708 #[stable(feature = "rust1", since = "1.0.0")]
709 impl<T
> Default
for Option
<T
> {
711 fn default() -> Option
<T
> { None }
714 #[stable(feature = "rust1", since = "1.0.0")]
715 impl<T
> IntoIterator
for Option
<T
> {
717 type IntoIter
= IntoIter
<T
>;
719 /// Returns a consuming iterator over the possibly contained value.
724 /// let x = Some("string");
725 /// let v: Vec<&str> = x.into_iter().collect();
726 /// assert_eq!(v, ["string"]);
729 /// let v: Vec<&str> = x.into_iter().collect();
730 /// assert!(v.is_empty());
733 fn into_iter(self) -> IntoIter
<T
> {
734 IntoIter { inner: Item { opt: self }
}
738 #[stable(since = "1.4.0", feature = "option_iter")]
739 impl<'a
, T
> IntoIterator
for &'a Option
<T
> {
741 type IntoIter
= Iter
<'a
, T
>;
743 fn into_iter(self) -> Iter
<'a
, T
> {
748 #[stable(since = "1.4.0", feature = "option_iter")]
749 impl<'a
, T
> IntoIterator
for &'a
mut Option
<T
> {
750 type Item
= &'a
mut T
;
751 type IntoIter
= IterMut
<'a
, T
>;
753 fn into_iter(mut self) -> IterMut
<'a
, T
> {
758 #[stable(since = "1.12.0", feature = "option_from")]
759 impl<T
> From
<T
> for Option
<T
> {
760 fn from(val
: T
) -> Option
<T
> {
765 /////////////////////////////////////////////////////////////////////////////
766 // The Option Iterators
767 /////////////////////////////////////////////////////////////////////////////
769 #[derive(Clone, Debug)]
774 impl<A
> Iterator
for Item
<A
> {
778 fn next(&mut self) -> Option
<A
> {
783 fn size_hint(&self) -> (usize, Option
<usize>) {
785 Some(_
) => (1, Some(1)),
786 None
=> (0, Some(0)),
791 impl<A
> DoubleEndedIterator
for Item
<A
> {
793 fn next_back(&mut self) -> Option
<A
> {
798 impl<A
> ExactSizeIterator
for Item
<A
> {}
800 /// An iterator over a reference of the contained item in an Option.
801 #[stable(feature = "rust1", since = "1.0.0")]
803 pub struct Iter
<'a
, A
: 'a
> { inner: Item<&'a A> }
805 #[stable(feature = "rust1", since = "1.0.0")]
806 impl<'a
, A
> Iterator
for Iter
<'a
, A
> {
810 fn next(&mut self) -> Option
<&'a A
> { self.inner.next() }
812 fn size_hint(&self) -> (usize, Option
<usize>) { self.inner.size_hint() }
815 #[stable(feature = "rust1", since = "1.0.0")]
816 impl<'a
, A
> DoubleEndedIterator
for Iter
<'a
, A
> {
818 fn next_back(&mut self) -> Option
<&'a A
> { self.inner.next_back() }
821 #[stable(feature = "rust1", since = "1.0.0")]
822 impl<'a
, A
> ExactSizeIterator
for Iter
<'a
, A
> {}
824 #[stable(feature = "rust1", since = "1.0.0")]
825 impl<'a
, A
> Clone
for Iter
<'a
, A
> {
826 fn clone(&self) -> Iter
<'a
, A
> {
827 Iter { inner: self.inner.clone() }
831 /// An iterator over a mutable reference of the contained item in an Option.
832 #[stable(feature = "rust1", since = "1.0.0")]
834 pub struct IterMut
<'a
, A
: 'a
> { inner: Item<&'a mut A> }
836 #[stable(feature = "rust1", since = "1.0.0")]
837 impl<'a
, A
> Iterator
for IterMut
<'a
, A
> {
838 type Item
= &'a
mut A
;
841 fn next(&mut self) -> Option
<&'a
mut A
> { self.inner.next() }
843 fn size_hint(&self) -> (usize, Option
<usize>) { self.inner.size_hint() }
846 #[stable(feature = "rust1", since = "1.0.0")]
847 impl<'a
, A
> DoubleEndedIterator
for IterMut
<'a
, A
> {
849 fn next_back(&mut self) -> Option
<&'a
mut A
> { self.inner.next_back() }
852 #[stable(feature = "rust1", since = "1.0.0")]
853 impl<'a
, A
> ExactSizeIterator
for IterMut
<'a
, A
> {}
855 /// An iterator over the item contained inside an Option.
856 #[derive(Clone, Debug)]
857 #[stable(feature = "rust1", since = "1.0.0")]
858 pub struct IntoIter
<A
> { inner: Item<A> }
860 #[stable(feature = "rust1", since = "1.0.0")]
861 impl<A
> Iterator
for IntoIter
<A
> {
865 fn next(&mut self) -> Option
<A
> { self.inner.next() }
867 fn size_hint(&self) -> (usize, Option
<usize>) { self.inner.size_hint() }
870 #[stable(feature = "rust1", since = "1.0.0")]
871 impl<A
> DoubleEndedIterator
for IntoIter
<A
> {
873 fn next_back(&mut self) -> Option
<A
> { self.inner.next_back() }
876 #[stable(feature = "rust1", since = "1.0.0")]
877 impl<A
> ExactSizeIterator
for IntoIter
<A
> {}
879 /////////////////////////////////////////////////////////////////////////////
881 /////////////////////////////////////////////////////////////////////////////
883 #[stable(feature = "rust1", since = "1.0.0")]
884 impl<A
, V
: FromIterator
<A
>> FromIterator
<Option
<A
>> for Option
<V
> {
885 /// Takes each element in the `Iterator`: if it is `None`, no further
886 /// elements are taken, and the `None` is returned. Should no `None` occur, a
887 /// container with the values of each `Option` is returned.
889 /// Here is an example which increments every integer in a vector,
890 /// checking for overflow:
895 /// let v = vec!(1, 2);
896 /// let res: Option<Vec<u16>> = v.iter().map(|&x: &u16|
897 /// if x == u16::MAX { None }
898 /// else { Some(x + 1) }
900 /// assert!(res == Some(vec!(2, 3)));
903 fn from_iter
<I
: IntoIterator
<Item
=Option
<A
>>>(iter
: I
) -> Option
<V
> {
904 // FIXME(#11084): This could be replaced with Iterator::scan when this
905 // performance bug is closed.
907 struct Adapter
<Iter
> {
912 impl<T
, Iter
: Iterator
<Item
=Option
<T
>>> Iterator
for Adapter
<Iter
> {
916 fn next(&mut self) -> Option
<T
> {
917 match self.iter
.next() {
918 Some(Some(value
)) => Some(value
),
920 self.found_none
= true;
928 let mut adapter
= Adapter { iter: iter.into_iter(), found_none: false }
;
929 let v
: V
= FromIterator
::from_iter(adapter
.by_ref());
931 if adapter
.found_none
{