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
97 //! Some(ref m) => println!("{}", *m),
101 //! // Remove the contained string, destroying the Option
102 //! let unwrapped_msg = match msg {
104 //! None => "default message",
108 //! Initialize a result to `None` before a loop:
111 //! enum Kingdom { Plant(u32, &'static str), Animal(u32, &'static str) }
113 //! // A list of data to search through.
114 //! let all_the_big_things = [
115 //! Kingdom::Plant(250, "redwood"),
116 //! Kingdom::Plant(230, "noble fir"),
117 //! Kingdom::Plant(229, "sugar pine"),
118 //! Kingdom::Animal(25, "blue whale"),
119 //! Kingdom::Animal(19, "fin whale"),
120 //! Kingdom::Animal(15, "north pacific right whale"),
123 //! // We're going to search for the name of the biggest animal,
124 //! // but to start with we've just got `None`.
125 //! let mut name_of_biggest_animal = None;
126 //! let mut size_of_biggest_animal = 0;
127 //! for big_thing in &all_the_big_things {
128 //! match *big_thing {
129 //! Kingdom::Animal(size, name) if size > size_of_biggest_animal => {
130 //! // Now we've found the name of some big animal
131 //! size_of_biggest_animal = size;
132 //! name_of_biggest_animal = Some(name);
134 //! Kingdom::Animal(..) | Kingdom::Plant(..) => ()
138 //! match name_of_biggest_animal {
139 //! Some(name) => println!("the biggest animal is {}", name),
140 //! None => println!("there are no animals :("),
144 #![stable(feature = "rust1", since = "1.0.0")]
150 use default::Default;
151 use iter::ExactSizeIterator;
152 use iter::{Iterator, DoubleEndedIterator, FromIterator, IntoIterator};
155 use result::Result::{Ok, Err};
159 // Note that this is not a lang item per se, but it has a hidden dependency on
160 // `Iterator`, which is one. The compiler assumes that the `next` method of
161 // `Iterator` is an enumeration with one type parameter and two variants,
162 // which basically means it must be `Option`.
164 /// The `Option` type. See [the module level documentation](index.html) for more.
165 #[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
166 #[stable(feature = "rust1", since = "1.0.0")]
169 #[stable(feature = "rust1", since = "1.0.0")]
172 #[stable(feature = "rust1", since = "1.0.0")]
176 /////////////////////////////////////////////////////////////////////////////
177 // Type implementation
178 /////////////////////////////////////////////////////////////////////////////
181 /////////////////////////////////////////////////////////////////////////
182 // Querying the contained values
183 /////////////////////////////////////////////////////////////////////////
185 /// Returns `true` if the option is a `Some` value
190 /// let x: Option<u32> = Some(2);
191 /// assert_eq!(x.is_some(), true);
193 /// let x: Option<u32> = None;
194 /// assert_eq!(x.is_some(), false);
197 #[stable(feature = "rust1", since = "1.0.0")]
198 pub fn is_some(&self) -> bool {
205 /// Returns `true` if the option is a `None` value
210 /// let x: Option<u32> = Some(2);
211 /// assert_eq!(x.is_none(), false);
213 /// let x: Option<u32> = None;
214 /// assert_eq!(x.is_none(), true);
217 #[stable(feature = "rust1", since = "1.0.0")]
218 pub fn is_none(&self) -> bool {
222 /////////////////////////////////////////////////////////////////////////
223 // Adapter for working with references
224 /////////////////////////////////////////////////////////////////////////
226 /// Converts from `Option<T>` to `Option<&T>`
230 /// Convert an `Option<String>` into an `Option<usize>`, preserving the original.
231 /// The `map` method takes the `self` argument by value, consuming the original,
232 /// so this technique uses `as_ref` to first take an `Option` to a reference
233 /// to the value inside the original.
236 /// let num_as_str: Option<String> = Some("10".to_string());
237 /// // First, cast `Option<String>` to `Option<&String>` with `as_ref`,
238 /// // then consume *that* with `map`, leaving `num_as_str` on the stack.
239 /// let num_as_int: Option<usize> = num_as_str.as_ref().map(|n| n.len());
240 /// println!("still can print num_as_str: {:?}", num_as_str);
243 #[stable(feature = "rust1", since = "1.0.0")]
244 pub fn as_ref(&self) -> Option<&T> {
246 Some(ref x) => Some(x),
251 /// Converts from `Option<T>` to `Option<&mut T>`
256 /// let mut x = Some(2);
257 /// match x.as_mut() {
258 /// Some(v) => *v = 42,
261 /// assert_eq!(x, Some(42));
264 #[stable(feature = "rust1", since = "1.0.0")]
265 pub fn as_mut(&mut self) -> Option<&mut T> {
267 Some(ref mut x) => Some(x),
272 /// Converts from `Option<T>` to `&mut [T]` (without copying)
277 /// #![feature(as_slice)]
279 /// let mut x = Some("Diamonds");
281 /// let v = x.as_mut_slice();
282 /// assert!(v == ["Diamonds"]);
284 /// assert!(v == ["Dirt"]);
286 /// assert_eq!(x, Some("Dirt"));
289 #[unstable(feature = "as_slice",
290 reason = "waiting for mut conventions",
292 #[deprecated(since = "1.4.0", reason = "niche API, unclear of usefulness")]
293 pub fn as_mut_slice(&mut self) -> &mut [T] {
296 let result: &mut [T] = slice::mut_ref_slice(x);
300 let result: &mut [T] = &mut [];
306 /////////////////////////////////////////////////////////////////////////
307 // Getting to contained values
308 /////////////////////////////////////////////////////////////////////////
310 /// Unwraps an option, yielding the content of a `Some`
314 /// Panics if the value is a `None` with a custom panic message provided by
320 /// let x = Some("value");
321 /// assert_eq!(x.expect("the world is ending"), "value");
324 /// ```{.should_panic}
325 /// let x: Option<&str> = None;
326 /// x.expect("the world is ending"); // panics with `the world is ending`
329 #[stable(feature = "rust1", since = "1.0.0")]
330 pub fn expect(self, msg: &str) -> T {
333 None => panic!("{}", msg),
337 /// Moves the value `v` out of the `Option<T>` if it is `Some(v)`.
341 /// Panics if the self value equals `None`.
345 /// In general, because this function may panic, its use is discouraged.
346 /// Instead, prefer to use pattern matching and handle the `None`
352 /// let x = Some("air");
353 /// assert_eq!(x.unwrap(), "air");
356 /// ```{.should_panic}
357 /// let x: Option<&str> = None;
358 /// assert_eq!(x.unwrap(), "air"); // fails
361 #[stable(feature = "rust1", since = "1.0.0")]
362 pub fn unwrap(self) -> T {
365 None => panic!("called `Option::unwrap()` on a `None` value"),
369 /// Returns the contained value or a default.
374 /// assert_eq!(Some("car").unwrap_or("bike"), "car");
375 /// assert_eq!(None.unwrap_or("bike"), "bike");
378 #[stable(feature = "rust1", since = "1.0.0")]
379 pub fn unwrap_or(self, def: T) -> T {
386 /// Returns the contained value or computes it from a closure.
392 /// assert_eq!(Some(4).unwrap_or_else(|| 2 * k), 4);
393 /// assert_eq!(None.unwrap_or_else(|| 2 * k), 20);
396 #[stable(feature = "rust1", since = "1.0.0")]
397 pub fn unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T {
404 /////////////////////////////////////////////////////////////////////////
405 // Transforming contained values
406 /////////////////////////////////////////////////////////////////////////
408 /// Maps an `Option<T>` to `Option<U>` by applying a function to a contained value
412 /// Convert an `Option<String>` into an `Option<usize>`, consuming the original:
415 /// let maybe_some_string = Some(String::from("Hello, World!"));
416 /// // `Option::map` takes self *by value*, consuming `maybe_some_string`
417 /// let maybe_some_len = maybe_some_string.map(|s| s.len());
419 /// assert_eq!(maybe_some_len, Some(13));
422 #[stable(feature = "rust1", since = "1.0.0")]
423 pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U> {
425 Some(x) => Some(f(x)),
430 /// Applies a function to the contained value (if any),
431 /// or returns a `default` (if not).
436 /// let x = Some("foo");
437 /// assert_eq!(x.map_or(42, |v| v.len()), 3);
439 /// let x: Option<&str> = None;
440 /// assert_eq!(x.map_or(42, |v| v.len()), 42);
443 #[stable(feature = "rust1", since = "1.0.0")]
444 pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U {
451 /// Applies a function to the contained value (if any),
452 /// or computes a `default` (if not).
459 /// let x = Some("foo");
460 /// assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 3);
462 /// let x: Option<&str> = None;
463 /// assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 42);
466 #[stable(feature = "rust1", since = "1.0.0")]
467 pub fn map_or_else<U, D: FnOnce() -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U {
474 /// Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to
475 /// `Ok(v)` and `None` to `Err(err)`.
480 /// let x = Some("foo");
481 /// assert_eq!(x.ok_or(0), Ok("foo"));
483 /// let x: Option<&str> = None;
484 /// assert_eq!(x.ok_or(0), Err(0));
487 #[stable(feature = "rust1", since = "1.0.0")]
488 pub fn ok_or<E>(self, err: E) -> Result<T, E> {
495 /// Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to
496 /// `Ok(v)` and `None` to `Err(err())`.
501 /// let x = Some("foo");
502 /// assert_eq!(x.ok_or_else(|| 0), Ok("foo"));
504 /// let x: Option<&str> = None;
505 /// assert_eq!(x.ok_or_else(|| 0), Err(0));
508 #[stable(feature = "rust1", since = "1.0.0")]
509 pub fn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<T, E> {
516 /////////////////////////////////////////////////////////////////////////
517 // Iterator constructors
518 /////////////////////////////////////////////////////////////////////////
520 /// Returns an iterator over the possibly contained value.
526 /// assert_eq!(x.iter().next(), Some(&4));
528 /// let x: Option<u32> = None;
529 /// assert_eq!(x.iter().next(), None);
532 #[stable(feature = "rust1", since = "1.0.0")]
533 pub fn iter(&self) -> Iter<T> {
534 Iter { inner: Item { opt: self.as_ref() } }
537 /// Returns a mutable iterator over the possibly contained value.
542 /// let mut x = Some(4);
543 /// match x.iter_mut().next() {
544 /// Some(v) => *v = 42,
547 /// assert_eq!(x, Some(42));
549 /// let mut x: Option<u32> = None;
550 /// assert_eq!(x.iter_mut().next(), None);
553 #[stable(feature = "rust1", since = "1.0.0")]
554 pub fn iter_mut(&mut self) -> IterMut<T> {
555 IterMut { inner: Item { opt: self.as_mut() } }
558 /////////////////////////////////////////////////////////////////////////
559 // Boolean operations on the values, eager and lazy
560 /////////////////////////////////////////////////////////////////////////
562 /// Returns `None` if the option is `None`, otherwise returns `optb`.
568 /// let y: Option<&str> = None;
569 /// assert_eq!(x.and(y), None);
571 /// let x: Option<u32> = None;
572 /// let y = Some("foo");
573 /// assert_eq!(x.and(y), None);
576 /// let y = Some("foo");
577 /// assert_eq!(x.and(y), Some("foo"));
579 /// let x: Option<u32> = None;
580 /// let y: Option<&str> = None;
581 /// assert_eq!(x.and(y), None);
584 #[stable(feature = "rust1", since = "1.0.0")]
585 pub fn and<U>(self, optb: Option<U>) -> Option<U> {
592 /// Returns `None` if the option is `None`, otherwise calls `f` with the
593 /// wrapped value and returns the result.
595 /// Some languages call this operation flatmap.
600 /// fn sq(x: u32) -> Option<u32> { Some(x * x) }
601 /// fn nope(_: u32) -> Option<u32> { None }
603 /// assert_eq!(Some(2).and_then(sq).and_then(sq), Some(16));
604 /// assert_eq!(Some(2).and_then(sq).and_then(nope), None);
605 /// assert_eq!(Some(2).and_then(nope).and_then(sq), None);
606 /// assert_eq!(None.and_then(sq).and_then(sq), None);
609 #[stable(feature = "rust1", since = "1.0.0")]
610 pub fn and_then<U, F: FnOnce(T) -> Option<U>>(self, f: F) -> Option<U> {
617 /// Returns the option if it contains a value, otherwise returns `optb`.
624 /// assert_eq!(x.or(y), Some(2));
627 /// let y = Some(100);
628 /// assert_eq!(x.or(y), Some(100));
631 /// let y = Some(100);
632 /// assert_eq!(x.or(y), Some(2));
634 /// let x: Option<u32> = None;
636 /// assert_eq!(x.or(y), None);
639 #[stable(feature = "rust1", since = "1.0.0")]
640 pub fn or(self, optb: Option<T>) -> Option<T> {
647 /// Returns the option if it contains a value, otherwise calls `f` and
648 /// returns the result.
653 /// fn nobody() -> Option<&'static str> { None }
654 /// fn vikings() -> Option<&'static str> { Some("vikings") }
656 /// assert_eq!(Some("barbarians").or_else(vikings), Some("barbarians"));
657 /// assert_eq!(None.or_else(vikings), Some("vikings"));
658 /// assert_eq!(None.or_else(nobody), None);
661 #[stable(feature = "rust1", since = "1.0.0")]
662 pub fn or_else<F: FnOnce() -> Option<T>>(self, f: F) -> Option<T> {
669 /////////////////////////////////////////////////////////////////////////
671 /////////////////////////////////////////////////////////////////////////
673 /// Takes the value out of the option, leaving a `None` in its place.
678 /// let mut x = Some(2);
680 /// assert_eq!(x, None);
682 /// let mut x: Option<u32> = None;
684 /// assert_eq!(x, None);
687 #[stable(feature = "rust1", since = "1.0.0")]
688 pub fn take(&mut self) -> Option<T> {
689 mem::replace(self, None)
692 /// Converts from `Option<T>` to `&[T]` (without copying)
694 #[unstable(feature = "as_slice", reason = "unsure of the utility here",
696 #[deprecated(since = "1.4.0", reason = "niche API, unclear of usefulness")]
697 pub fn as_slice(&self) -> &[T] {
699 Some(ref x) => slice::ref_slice(x),
701 let result: &[_] = &[];
708 impl<'a, T: Clone> Option<&'a T> {
709 /// Maps an Option<&T> to an Option<T> by cloning the contents of the Option.
710 #[stable(feature = "rust1", since = "1.0.0")]
711 pub fn cloned(self) -> Option<T> {
712 self.map(|t| t.clone())
716 impl<T: Default> Option<T> {
717 /// Returns the contained value or a default
719 /// Consumes the `self` argument then, if `Some`, returns the contained
720 /// value, otherwise if `None`, returns the default value for that
725 /// Convert a string to an integer, turning poorly-formed strings
726 /// into 0 (the default value for integers). `parse` converts
727 /// a string to any other type that implements `FromStr`, returning
731 /// let good_year_from_input = "1909";
732 /// let bad_year_from_input = "190blarg";
733 /// let good_year = good_year_from_input.parse().ok().unwrap_or_default();
734 /// let bad_year = bad_year_from_input.parse().ok().unwrap_or_default();
736 /// assert_eq!(1909, good_year);
737 /// assert_eq!(0, bad_year);
740 #[stable(feature = "rust1", since = "1.0.0")]
741 pub fn unwrap_or_default(self) -> T {
744 None => Default::default(),
749 /////////////////////////////////////////////////////////////////////////////
750 // Trait implementations
751 /////////////////////////////////////////////////////////////////////////////
753 #[stable(feature = "rust1", since = "1.0.0")]
754 impl<T> Default for Option<T> {
756 #[stable(feature = "rust1", since = "1.0.0")]
757 fn default() -> Option<T> { None }
760 #[stable(feature = "rust1", since = "1.0.0")]
761 impl<T> IntoIterator for Option<T> {
763 type IntoIter = IntoIter<T>;
765 /// Returns a consuming iterator over the possibly contained value.
770 /// let x = Some("string");
771 /// let v: Vec<&str> = x.into_iter().collect();
772 /// assert_eq!(v, ["string"]);
775 /// let v: Vec<&str> = x.into_iter().collect();
776 /// assert!(v.is_empty());
779 fn into_iter(self) -> IntoIter<T> {
780 IntoIter { inner: Item { opt: self } }
784 #[stable(since = "1.4.0", feature = "option_iter")]
785 impl<'a, T> IntoIterator for &'a Option<T> {
787 type IntoIter = Iter<'a, T>;
789 fn into_iter(self) -> Iter<'a, T> {
794 #[stable(since = "1.4.0", feature = "option_iter")]
795 impl<'a, T> IntoIterator for &'a mut Option<T> {
796 type Item = &'a mut T;
797 type IntoIter = IterMut<'a, T>;
799 fn into_iter(mut self) -> IterMut<'a, T> {
804 /////////////////////////////////////////////////////////////////////////////
805 // The Option Iterators
806 /////////////////////////////////////////////////////////////////////////////
813 impl<A> Iterator for Item<A> {
817 fn next(&mut self) -> Option<A> {
822 fn size_hint(&self) -> (usize, Option<usize>) {
824 Some(_) => (1, Some(1)),
825 None => (0, Some(0)),
830 impl<A> DoubleEndedIterator for Item<A> {
832 fn next_back(&mut self) -> Option<A> {
837 impl<A> ExactSizeIterator for Item<A> {}
839 /// An iterator over a reference of the contained item in an Option.
840 #[stable(feature = "rust1", since = "1.0.0")]
841 pub struct Iter<'a, A: 'a> { inner: Item<&'a A> }
843 #[stable(feature = "rust1", since = "1.0.0")]
844 impl<'a, A> Iterator for Iter<'a, A> {
848 fn next(&mut self) -> Option<&'a A> { self.inner.next() }
850 fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
853 #[stable(feature = "rust1", since = "1.0.0")]
854 impl<'a, A> DoubleEndedIterator for Iter<'a, A> {
856 fn next_back(&mut self) -> Option<&'a A> { self.inner.next_back() }
859 #[stable(feature = "rust1", since = "1.0.0")]
860 impl<'a, A> ExactSizeIterator for Iter<'a, A> {}
862 #[stable(feature = "rust1", since = "1.0.0")]
863 impl<'a, A> Clone for Iter<'a, A> {
864 fn clone(&self) -> Iter<'a, A> {
865 Iter { inner: self.inner.clone() }
869 /// An iterator over a mutable reference of the contained item in an Option.
870 #[stable(feature = "rust1", since = "1.0.0")]
871 pub struct IterMut<'a, A: 'a> { inner: Item<&'a mut A> }
873 #[stable(feature = "rust1", since = "1.0.0")]
874 impl<'a, A> Iterator for IterMut<'a, A> {
875 type Item = &'a mut A;
878 fn next(&mut self) -> Option<&'a mut A> { self.inner.next() }
880 fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
883 #[stable(feature = "rust1", since = "1.0.0")]
884 impl<'a, A> DoubleEndedIterator for IterMut<'a, A> {
886 fn next_back(&mut self) -> Option<&'a mut A> { self.inner.next_back() }
889 #[stable(feature = "rust1", since = "1.0.0")]
890 impl<'a, A> ExactSizeIterator for IterMut<'a, A> {}
892 /// An iterator over the item contained inside an Option.
894 #[stable(feature = "rust1", since = "1.0.0")]
895 pub struct IntoIter<A> { inner: Item<A> }
897 #[stable(feature = "rust1", since = "1.0.0")]
898 impl<A> Iterator for IntoIter<A> {
902 fn next(&mut self) -> Option<A> { self.inner.next() }
904 fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
907 #[stable(feature = "rust1", since = "1.0.0")]
908 impl<A> DoubleEndedIterator for IntoIter<A> {
910 fn next_back(&mut self) -> Option<A> { self.inner.next_back() }
913 #[stable(feature = "rust1", since = "1.0.0")]
914 impl<A> ExactSizeIterator for IntoIter<A> {}
916 /////////////////////////////////////////////////////////////////////////////
918 /////////////////////////////////////////////////////////////////////////////
920 #[stable(feature = "rust1", since = "1.0.0")]
921 impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
922 /// Takes each element in the `Iterator`: if it is `None`, no further
923 /// elements are taken, and the `None` is returned. Should no `None` occur, a
924 /// container with the values of each `Option` is returned.
926 /// Here is an example which increments every integer in a vector,
927 /// checking for overflow:
932 /// let v = vec!(1, 2);
933 /// let res: Option<Vec<u16>> = v.iter().map(|&x: &u16|
934 /// if x == u16::MAX { None }
935 /// else { Some(x + 1) }
937 /// assert!(res == Some(vec!(2, 3)));
940 #[stable(feature = "rust1", since = "1.0.0")]
941 fn from_iter<I: IntoIterator<Item=Option<A>>>(iter: I) -> Option<V> {
942 // FIXME(#11084): This could be replaced with Iterator::scan when this
943 // performance bug is closed.
945 struct Adapter<Iter> {
950 impl<T, Iter: Iterator<Item=Option<T>>> Iterator for Adapter<Iter> {
954 fn next(&mut self) -> Option<T> {
955 match self.iter.next() {
956 Some(Some(value)) => Some(value),
958 self.found_none = true;
966 let mut adapter = Adapter { iter: iter.into_iter(), found_none: false };
967 let v: V = FromIterator::from_iter(adapter.by_ref());
969 if adapter.found_none {