]> git.lizzy.rs Git - rust.git/blob - src/libstd/option.rs
switch Drop to `&mut self`
[rust.git] / src / libstd / option.rs
1 // Copyright 2012-2013 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.
4 //
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.
10
11 /*!
12
13 Operations on the ubiquitous `Option` type.
14
15 Type `Option` represents an optional value.
16
17 Every `Option<T>` value can either be `Some(T)` or `None`. Where in other
18 languages you might use a nullable type, in Rust you would use an option
19 type.
20
21 Options are most commonly used with pattern matching to query the presence
22 of a value and take action, always accounting for the `None` case.
23
24 # Example
25
26 ~~~
27 let msg = Some(~"howdy");
28
29 // Take a reference to the contained string
30 match msg {
31     Some(ref m) => io::println(m),
32     None => ()
33 }
34
35 // Remove the contained string, destroying the Option
36 let unwrapped_msg = match msg {
37     Some(m) => m,
38     None => ~"default message"
39 };
40 ~~~
41
42 */
43
44 use clone::Clone;
45 use cmp::{Eq,Ord};
46 use default::Default;
47 use either;
48 use util;
49 use num::Zero;
50 use iter;
51 use iter::{Iterator, DoubleEndedIterator, ExactSize};
52 use result;
53 use str::{StrSlice, OwnedStr};
54 use to_str::ToStr;
55 use clone::DeepClone;
56
57 /// The option type
58 #[deriving(Clone, DeepClone, Eq)]
59 pub enum Option<T> {
60     None,
61     Some(T),
62 }
63
64 impl<T: Eq + Ord> Ord for Option<T> {
65     fn lt(&self, other: &Option<T>) -> bool {
66         iter::order::lt(self.iter(), other.iter())
67     }
68
69     fn le(&self, other: &Option<T>) -> bool {
70         iter::order::le(self.iter(), other.iter())
71     }
72
73     fn ge(&self, other: &Option<T>) -> bool {
74         iter::order::ge(self.iter(), other.iter())
75     }
76
77     fn gt(&self, other: &Option<T>) -> bool {
78         iter::order::gt(self.iter(), other.iter())
79     }
80 }
81
82 // FIXME: #8242 implementing manually because deriving doesn't work for some reason
83 impl<T: ToStr> ToStr for Option<T> {
84     fn to_str(&self) -> ~str {
85         match *self {
86             Some(ref x) => {
87                 let mut s = ~"Some(";
88                 s.push_str(x.to_str());
89                 s.push_str(")");
90                 s
91             }
92             None => ~"None"
93         }
94     }
95 }
96
97 impl<T> Option<T> {
98     /// Return an iterator over the possibly contained value
99     #[inline]
100     pub fn iter<'r>(&'r self) -> OptionIterator<&'r T> {
101         match *self {
102             Some(ref x) => OptionIterator{opt: Some(x)},
103             None => OptionIterator{opt: None}
104         }
105     }
106
107     /// Return a mutable iterator over the possibly contained value
108     #[inline]
109     pub fn mut_iter<'r>(&'r mut self) -> OptionIterator<&'r mut T> {
110         match *self {
111             Some(ref mut x) => OptionIterator{opt: Some(x)},
112             None => OptionIterator{opt: None}
113         }
114     }
115
116     /// Return a consuming iterator over the possibly contained value
117     #[inline]
118     pub fn move_iter(self) -> OptionIterator<T> {
119         OptionIterator{opt: self}
120     }
121
122     /// Returns true if the option equals `None`
123     #[inline]
124     pub fn is_none(&self) -> bool {
125         match *self { None => true, Some(_) => false }
126     }
127
128     /// Returns true if the option contains a `Some` value
129     #[inline]
130     pub fn is_some(&self) -> bool { !self.is_none() }
131
132     /// Returns `None` if the option is `None`, otherwise returns `optb`.
133     #[inline]
134     pub fn and(self, optb: Option<T>) -> Option<T> {
135         match self {
136             Some(_) => optb,
137             None => None,
138         }
139     }
140
141     /// Returns `None` if the option is `None`, otherwise calls `f` with the
142     /// wrapped value and returns the result.
143     #[inline]
144     pub fn and_then<U>(self, f: &fn(T) -> Option<U>) -> Option<U> {
145         match self {
146             Some(x) => f(x),
147             None => None,
148         }
149     }
150
151     /// Returns `None` if the option is `None`, otherwise calls `f` with a
152     /// reference to the wrapped value and returns the result.
153     #[inline]
154     pub fn and_then_ref<'a, U>(&'a self, f: &fn(&'a T) -> Option<U>) -> Option<U> {
155         match *self {
156             Some(ref x) => f(x),
157             None => None
158         }
159     }
160
161     /// Returns `None` if the option is `None`, otherwise calls `f` with a
162     /// mutable reference to the wrapped value and returns the result.
163     #[inline]
164     pub fn and_then_mut_ref<'a, U>(&'a mut self, f: &fn(&'a mut T) -> Option<U>) -> Option<U> {
165         match *self {
166             Some(ref mut x) => f(x),
167             None => None
168         }
169     }
170
171     /// Returns the option if it contains a value, otherwise returns `optb`.
172     #[inline]
173     pub fn or(self, optb: Option<T>) -> Option<T> {
174         match self {
175             Some(_) => self,
176             None => optb
177         }
178     }
179
180     /// Returns the option if it contains a value, otherwise calls `f` and
181     /// returns the result.
182     #[inline]
183     pub fn or_else(self, f: &fn() -> Option<T>) -> Option<T> {
184         match self {
185             Some(_) => self,
186             None => f(),
187         }
188     }
189
190     /// Filters an optional value using given function.
191     #[inline(always)]
192     pub fn filtered(self, f: &fn(t: &T) -> bool) -> Option<T> {
193         match self {
194             Some(x) => if(f(&x)) {Some(x)} else {None},
195             None => None
196         }
197     }
198
199     /// Maps a `Some` value from one type to another by reference
200     #[inline]
201     pub fn map<'a, U>(&'a self, f: &fn(&'a T) -> U) -> Option<U> {
202         match *self { Some(ref x) => Some(f(x)), None => None }
203     }
204
205     /// Maps a `Some` value from one type to another by a mutable reference
206     #[inline]
207     pub fn map_mut<'a, U>(&'a mut self, f: &fn(&'a mut T) -> U) -> Option<U> {
208         match *self { Some(ref mut x) => Some(f(x)), None => None }
209     }
210
211     /// Applies a function to the contained value or returns a default
212     #[inline]
213     pub fn map_default<'a, U>(&'a self, def: U, f: &fn(&'a T) -> U) -> U {
214         match *self { None => def, Some(ref t) => f(t) }
215     }
216
217     /// Maps a `Some` value from one type to another by a mutable reference,
218     /// or returns a default value.
219     #[inline]
220     pub fn map_mut_default<'a, U>(&'a mut self, def: U, f: &fn(&'a mut T) -> U) -> U {
221         match *self { Some(ref mut x) => f(x), None => def }
222     }
223
224     /// As `map`, but consumes the option and gives `f` ownership to avoid
225     /// copying.
226     #[inline]
227     pub fn map_move<U>(self, f: &fn(T) -> U) -> Option<U> {
228         match self { Some(x) => Some(f(x)), None => None }
229     }
230
231     /// As `map_default`, but consumes the option and gives `f`
232     /// ownership to avoid copying.
233     #[inline]
234     pub fn map_move_default<U>(self, def: U, f: &fn(T) -> U) -> U {
235         match self { None => def, Some(t) => f(t) }
236     }
237
238     /// Take the value out of the option, leaving a `None` in its place.
239     #[inline]
240     pub fn take(&mut self) -> Option<T> {
241         util::replace(self, None)
242     }
243
244     /// Apply a function to the contained value or do nothing.
245     /// Returns true if the contained value was mutated.
246     pub fn mutate(&mut self, f: &fn(T) -> T) -> bool {
247         if self.is_some() {
248             *self = Some(f(self.take_unwrap()));
249             true
250         } else { false }
251     }
252
253     /// Apply a function to the contained value or set it to a default.
254     /// Returns true if the contained value was mutated, or false if set to the default.
255     pub fn mutate_default(&mut self, def: T, f: &fn(T) -> T) -> bool {
256         if self.is_some() {
257             *self = Some(f(self.take_unwrap()));
258             true
259         } else {
260             *self = Some(def);
261             false
262         }
263     }
264
265     /// Gets an immutable reference to the value inside an option.
266     ///
267     /// # Failure
268     ///
269     /// Fails if the value equals `None`
270     ///
271     /// # Safety note
272     ///
273     /// In general, because this function may fail, its use is discouraged
274     /// (calling `get` on `None` is akin to dereferencing a null pointer).
275     /// Instead, prefer to use pattern matching and handle the `None`
276     /// case explicitly.
277     #[inline]
278     pub fn get_ref<'a>(&'a self) -> &'a T {
279         match *self {
280             Some(ref x) => x,
281             None => fail!("called `Option::get_ref()` on a `None` value"),
282         }
283     }
284
285     /// Gets a mutable reference to the value inside an option.
286     ///
287     /// # Failure
288     ///
289     /// Fails if the value equals `None`
290     ///
291     /// # Safety note
292     ///
293     /// In general, because this function may fail, its use is discouraged
294     /// (calling `get` on `None` is akin to dereferencing a null pointer).
295     /// Instead, prefer to use pattern matching and handle the `None`
296     /// case explicitly.
297     #[inline]
298     pub fn get_mut_ref<'a>(&'a mut self) -> &'a mut T {
299         match *self {
300             Some(ref mut x) => x,
301             None => fail!("called `Option::get_mut_ref()` on a `None` value"),
302         }
303     }
304
305     /// Moves a value out of an option type and returns it.
306     ///
307     /// Useful primarily for getting strings, vectors and unique pointers out
308     /// of option types without copying them.
309     ///
310     /// # Failure
311     ///
312     /// Fails if the value equals `None`.
313     ///
314     /// # Safety note
315     ///
316     /// In general, because this function may fail, its use is discouraged.
317     /// Instead, prefer to use pattern matching and handle the `None`
318     /// case explicitly.
319     #[inline]
320     pub fn unwrap(self) -> T {
321         match self {
322             Some(x) => x,
323             None => fail!("called `Option::unwrap()` on a `None` value"),
324         }
325     }
326
327     /// The option dance. Moves a value out of an option type and returns it,
328     /// replacing the original with `None`.
329     ///
330     /// # Failure
331     ///
332     /// Fails if the value equals `None`.
333     #[inline]
334     pub fn take_unwrap(&mut self) -> T {
335         if self.is_none() {
336             fail!("called `Option::take_unwrap()` on a `None` value")
337         }
338         self.take().unwrap()
339     }
340
341     ///  Gets the value out of an option, printing a specified message on
342     ///  failure
343     ///
344     ///  # Failure
345     ///
346     ///  Fails if the value equals `None`
347     #[inline]
348     pub fn expect(self, reason: &str) -> T {
349         match self {
350             Some(val) => val,
351             None => fail!(reason.to_owned()),
352         }
353     }
354
355     /// Returns the contained value or a default
356     #[inline]
357     pub fn unwrap_or(self, def: T) -> T {
358         match self {
359             Some(x) => x,
360             None => def
361         }
362     }
363
364     /// Returns the contained value or computes it from a closure
365     #[inline]
366     pub fn unwrap_or_else(self, f: &fn() -> T) -> T {
367         match self {
368             Some(x) => x,
369             None => f()
370         }
371     }
372
373     /// Applies a function zero or more times until the result is `None`.
374     #[inline]
375     pub fn while_some(self, blk: &fn(v: T) -> Option<T>) {
376         let mut opt = self;
377         while opt.is_some() {
378             opt = blk(opt.unwrap());
379         }
380     }
381 }
382
383 /// A generic trait for converting a value to a `Option`
384 pub trait ToOption<T> {
385     /// Convert to the `option` type
386     fn to_option(&self) -> Option<T>;
387 }
388
389 /// A generic trait for converting a value to a `Option`
390 pub trait IntoOption<T> {
391     /// Convert to the `option` type
392     fn into_option(self) -> Option<T>;
393 }
394
395 /// A generic trait for converting a value to a `Option`
396 pub trait AsOption<T> {
397     /// Convert to the `option` type
398     fn as_option<'a>(&'a self) -> Option<&'a T>;
399 }
400
401 impl<T: Clone> ToOption<T> for Option<T> {
402     #[inline]
403     fn to_option(&self) -> Option<T> { self.clone() }
404 }
405
406 impl<T> IntoOption<T> for Option<T> {
407     #[inline]
408     fn into_option(self) -> Option<T> { self }
409 }
410
411 impl<T> AsOption<T> for Option<T> {
412     #[inline]
413     fn as_option<'a>(&'a self) -> Option<&'a T> {
414         match *self {
415             Some(ref x) => Some(x),
416             None => None,
417         }
418     }
419 }
420
421 impl<T: Clone> result::ToResult<T, ()> for Option<T> {
422     #[inline]
423     fn to_result(&self) -> result::Result<T, ()> {
424         match *self {
425             Some(ref x) => result::Ok(x.clone()),
426             None => result::Err(()),
427         }
428     }
429 }
430
431 impl<T> result::IntoResult<T, ()> for Option<T> {
432     #[inline]
433     fn into_result(self) -> result::Result<T, ()> {
434         match self {
435             Some(x) => result::Ok(x),
436             None => result::Err(()),
437         }
438     }
439 }
440
441 impl<T: Clone> either::ToEither<(), T> for Option<T> {
442     #[inline]
443     fn to_either(&self) -> either::Either<(), T> {
444         match *self {
445             Some(ref x) => either::Right(x.clone()),
446             None => either::Left(()),
447         }
448     }
449 }
450
451 impl<T> either::IntoEither<(), T> for Option<T> {
452     #[inline]
453     fn into_either(self) -> either::Either<(), T> {
454         match self {
455             Some(x) => either::Right(x),
456             None => either::Left(()),
457         }
458     }
459 }
460
461 impl<T: Default> Option<T> {
462     /// Returns the contained value or default (for this type)
463     #[inline]
464     pub fn unwrap_or_default(self) -> T {
465         match self {
466             Some(x) => x,
467             None => Default::default()
468         }
469     }
470
471     /// Returns self or `Some`-wrapped default value
472     #[inline]
473     pub fn or_default(self) -> Option<T> {
474         match self {
475             None => Some(Default::default()),
476             x => x,
477         }
478     }
479 }
480
481 impl<T> Default for Option<T> {
482     #[inline]
483     fn default() -> Option<T> { None }
484 }
485
486 impl<T:Zero> Option<T> {
487     /// Returns the contained value or zero (for this type)
488     #[inline]
489     pub fn unwrap_or_zero(self) -> T {
490         match self {
491             Some(x) => x,
492             None => Zero::zero()
493         }
494     }
495
496     /// Returns self or `Some`-wrapped zero value
497     #[inline]
498     pub fn or_zero(self) -> Option<T> {
499         match self {
500             None => Some(Zero::zero()),
501             x => x
502         }
503     }
504 }
505
506 /// An iterator that yields either one or zero elements
507 #[deriving(Clone, DeepClone)]
508 pub struct OptionIterator<A> {
509     priv opt: Option<A>
510 }
511
512 impl<A> Iterator<A> for OptionIterator<A> {
513     #[inline]
514     fn next(&mut self) -> Option<A> {
515         self.opt.take()
516     }
517
518     #[inline]
519     fn size_hint(&self) -> (uint, Option<uint>) {
520         match self.opt {
521             Some(_) => (1, Some(1)),
522             None => (0, Some(0)),
523         }
524     }
525 }
526
527 impl<A> DoubleEndedIterator<A> for OptionIterator<A> {
528     #[inline]
529     fn next_back(&mut self) -> Option<A> {
530         self.opt.take()
531     }
532 }
533
534 impl<A> ExactSize<A> for OptionIterator<A> {}
535
536 #[cfg(test)]
537 mod tests {
538     use super::*;
539
540     use either::{IntoEither, ToEither};
541     use either;
542     use result::{IntoResult, ToResult};
543     use result;
544     use util;
545
546     #[test]
547     fn test_get_ptr() {
548         unsafe {
549             let x = ~0;
550             let addr_x: *int = ::cast::transmute(&*x);
551             let opt = Some(x);
552             let y = opt.unwrap();
553             let addr_y: *int = ::cast::transmute(&*y);
554             assert_eq!(addr_x, addr_y);
555         }
556     }
557
558     #[test]
559     fn test_get_str() {
560         let x = ~"test";
561         let addr_x = x.as_imm_buf(|buf, _len| buf);
562         let opt = Some(x);
563         let y = opt.unwrap();
564         let addr_y = y.as_imm_buf(|buf, _len| buf);
565         assert_eq!(addr_x, addr_y);
566     }
567
568     #[test]
569     fn test_get_resource() {
570         struct R {
571            i: @mut int,
572         }
573
574         #[unsafe_destructor]
575         impl ::ops::Drop for R {
576            fn drop(&mut self) { *(self.i) += 1; }
577         }
578
579         fn R(i: @mut int) -> R {
580             R {
581                 i: i
582             }
583         }
584
585         let i = @mut 0;
586         {
587             let x = R(i);
588             let opt = Some(x);
589             let _y = opt.unwrap();
590         }
591         assert_eq!(*i, 1);
592     }
593
594     #[test]
595     fn test_option_dance() {
596         let x = Some(());
597         let mut y = Some(5);
598         let mut y2 = 0;
599         for _x in x.iter() {
600             y2 = y.take_unwrap();
601         }
602         assert_eq!(y2, 5);
603         assert!(y.is_none());
604     }
605     #[test] #[should_fail]
606     fn test_option_too_much_dance() {
607         let mut y = Some(util::NonCopyable);
608         let _y2 = y.take_unwrap();
609         let _y3 = y.take_unwrap();
610     }
611
612     #[test]
613     fn test_and() {
614         let x: Option<int> = Some(1);
615         assert_eq!(x.and(Some(2)), Some(2));
616         assert_eq!(x.and(None), None);
617
618         let x: Option<int> = None;
619         assert_eq!(x.and(Some(2)), None);
620         assert_eq!(x.and(None), None);
621     }
622
623     #[test]
624     fn test_and_then() {
625         let x: Option<int> = Some(1);
626         assert_eq!(x.and_then(|x| Some(x + 1)), Some(2));
627         assert_eq!(x.and_then(|_| None::<int>), None);
628
629         let x: Option<int> = None;
630         assert_eq!(x.and_then(|x| Some(x + 1)), None);
631         assert_eq!(x.and_then(|_| None::<int>), None);
632     }
633
634     #[test]
635     fn test_or() {
636         let x: Option<int> = Some(1);
637         assert_eq!(x.or(Some(2)), Some(1));
638         assert_eq!(x.or(None), Some(1));
639
640         let x: Option<int> = None;
641         assert_eq!(x.or(Some(2)), Some(2));
642         assert_eq!(x.or(None), None);
643     }
644
645     #[test]
646     fn test_or_else() {
647         let x: Option<int> = Some(1);
648         assert_eq!(x.or_else(|| Some(2)), Some(1));
649         assert_eq!(x.or_else(|| None), Some(1));
650
651         let x: Option<int> = None;
652         assert_eq!(x.or_else(|| Some(2)), Some(2));
653         assert_eq!(x.or_else(|| None), None);
654     }
655
656     #[test]
657     fn test_option_while_some() {
658         let mut i = 0;
659         do Some(10).while_some |j| {
660             i += 1;
661             if (j > 0) {
662                 Some(j-1)
663             } else {
664                 None
665             }
666         }
667         assert_eq!(i, 11);
668     }
669
670     #[test]
671     fn test_unwrap() {
672         assert_eq!(Some(1).unwrap(), 1);
673         assert_eq!(Some(~"hello").unwrap(), ~"hello");
674     }
675
676     #[test]
677     #[should_fail]
678     fn test_unwrap_fail1() {
679         let x: Option<int> = None;
680         x.unwrap();
681     }
682
683     #[test]
684     #[should_fail]
685     fn test_unwrap_fail2() {
686         let x: Option<~str> = None;
687         x.unwrap();
688     }
689
690     #[test]
691     fn test_unwrap_or() {
692         let x: Option<int> = Some(1);
693         assert_eq!(x.unwrap_or(2), 1);
694
695         let x: Option<int> = None;
696         assert_eq!(x.unwrap_or(2), 2);
697     }
698
699     #[test]
700     fn test_unwrap_or_else() {
701         let x: Option<int> = Some(1);
702         assert_eq!(x.unwrap_or_else(|| 2), 1);
703
704         let x: Option<int> = None;
705         assert_eq!(x.unwrap_or_else(|| 2), 2);
706     }
707
708     #[test]
709     fn test_unwrap_or_zero() {
710         let some_stuff = Some(42);
711         assert_eq!(some_stuff.unwrap_or_zero(), 42);
712         let no_stuff: Option<int> = None;
713         assert_eq!(no_stuff.unwrap_or_zero(), 0);
714     }
715
716     #[test]
717     fn test_filtered() {
718         let some_stuff = Some(42);
719         let modified_stuff = some_stuff.filtered(|&x| {x < 10});
720         assert_eq!(some_stuff.unwrap(), 42);
721         assert!(modified_stuff.is_none());
722     }
723
724     #[test]
725     fn test_iter() {
726         let val = 5;
727
728         let x = Some(val);
729         let mut it = x.iter();
730
731         assert_eq!(it.size_hint(), (1, Some(1)));
732         assert_eq!(it.next(), Some(&val));
733         assert_eq!(it.size_hint(), (0, Some(0)));
734         assert!(it.next().is_none());
735     }
736
737     #[test]
738     fn test_mut_iter() {
739         let val = 5;
740         let new_val = 11;
741
742         let mut x = Some(val);
743         let mut it = x.mut_iter();
744
745         assert_eq!(it.size_hint(), (1, Some(1)));
746
747         match it.next() {
748             Some(interior) => {
749                 assert_eq!(*interior, val);
750                 *interior = new_val;
751                 assert_eq!(x, Some(new_val));
752             }
753             None => assert!(false),
754         }
755
756         assert_eq!(it.size_hint(), (0, Some(0)));
757         assert!(it.next().is_none());
758     }
759
760     #[test]
761     fn test_ord() {
762         let small = Some(1.0);
763         let big = Some(5.0);
764         let nan = Some(0.0/0.0);
765         assert!(!(nan < big));
766         assert!(!(nan > big));
767         assert!(small < big);
768         assert!(None < big);
769         assert!(big > None);
770     }
771
772     #[test]
773     fn test_mutate() {
774         let mut x = Some(3i);
775         assert!(x.mutate(|i| i+1));
776         assert_eq!(x, Some(4i));
777         assert!(x.mutate_default(0, |i| i+1));
778         assert_eq!(x, Some(5i));
779         x = None;
780         assert!(!x.mutate(|i| i+1));
781         assert_eq!(x, None);
782         assert!(!x.mutate_default(0i, |i| i+1));
783         assert_eq!(x, Some(0i));
784     }
785
786     #[test]
787     pub fn test_to_option() {
788         let some: Option<int> = Some(100);
789         let none: Option<int> = None;
790
791         assert_eq!(some.to_option(), Some(100));
792         assert_eq!(none.to_option(), None);
793     }
794
795     #[test]
796     pub fn test_into_option() {
797         let some: Option<int> = Some(100);
798         let none: Option<int> = None;
799
800         assert_eq!(some.into_option(), Some(100));
801         assert_eq!(none.into_option(), None);
802     }
803
804     #[test]
805     pub fn test_as_option() {
806         let some: Option<int> = Some(100);
807         let none: Option<int> = None;
808
809         assert_eq!(some.as_option().unwrap(), &100);
810         assert_eq!(none.as_option(), None);
811     }
812
813     #[test]
814     pub fn test_to_result() {
815         let some: Option<int> = Some(100);
816         let none: Option<int> = None;
817
818         assert_eq!(some.to_result(), result::Ok(100));
819         assert_eq!(none.to_result(), result::Err(()));
820     }
821
822     #[test]
823     pub fn test_into_result() {
824         let some: Option<int> = Some(100);
825         let none: Option<int> = None;
826
827         assert_eq!(some.into_result(), result::Ok(100));
828         assert_eq!(none.into_result(), result::Err(()));
829     }
830
831     #[test]
832     pub fn test_to_either() {
833         let some: Option<int> = Some(100);
834         let none: Option<int> = None;
835
836         assert_eq!(some.to_either(), either::Right(100));
837         assert_eq!(none.to_either(), either::Left(()));
838     }
839
840     #[test]
841     pub fn test_into_either() {
842         let some: Option<int> = Some(100);
843         let none: Option<int> = None;
844
845         assert_eq!(some.into_either(), either::Right(100));
846         assert_eq!(none.into_either(), either::Left(()));
847     }
848 }