Skip to content

Commit 04da11a

Browse files
committed
---
yaml --- r: 80724 b: refs/heads/try c: 7380b1c h: refs/heads/master v: v3
1 parent 9419308 commit 04da11a

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 4c6bf4872012c010f84dc7fa2cdfe87522533f89
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: cbd1eefbd350797b783df119fed7956d7e1c74ad
5-
refs/heads/try: 2bd87ad432901f3baeab2fc1f2aa8aa328f04ea7
5+
refs/heads/try: 7380b1ce7f7538ca6f6f61cc442f45e64622b902
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libstd/option.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,15 @@ impl<T> Option<T> {
340340
}
341341
}
342342

343+
/// Returns the contained value or computes it from a closure
344+
#[inline]
345+
pub fn unwrap_or_else(self, f: &fn() -> T) -> T {
346+
match self {
347+
Some(x) => x,
348+
None => f()
349+
}
350+
}
351+
343352
/// Applies a function zero or more times until the result is `None`.
344353
#[inline]
345354
pub fn while_some(self, blk: &fn(v: T) -> Option<T>) {
@@ -514,6 +523,44 @@ mod tests {
514523
assert_eq!(i, 11);
515524
}
516525

526+
#[test]
527+
fn test_unwrap() {
528+
assert_eq!(Some(1).unwrap(), 1);
529+
assert_eq!(Some(~"hello").unwrap(), ~"hello");
530+
}
531+
532+
#[test]
533+
#[should_fail]
534+
fn test_unwrap_fail1() {
535+
let x: Option<int> = None;
536+
x.unwrap();
537+
}
538+
539+
#[test]
540+
#[should_fail]
541+
fn test_unwrap_fail2() {
542+
let x: Option<~str> = None;
543+
x.unwrap();
544+
}
545+
546+
#[test]
547+
fn test_unwrap_or() {
548+
let x: Option<int> = Some(1);
549+
assert_eq!(x.unwrap_or(2), 1);
550+
551+
let x: Option<int> = None;
552+
assert_eq!(x.unwrap_or(2), 2);
553+
}
554+
555+
#[test]
556+
fn test_unwrap_or_else() {
557+
let x: Option<int> = Some(1);
558+
assert_eq!(x.unwrap_or_else(|| 2), 1);
559+
560+
let x: Option<int> = None;
561+
assert_eq!(x.unwrap_or_else(|| 2), 2);
562+
}
563+
517564
#[test]
518565
fn test_unwrap_or_zero() {
519566
let some_stuff = Some(42);

0 commit comments

Comments
 (0)