T?
optional T?
[src]
pub fn expect(self, message: string): T {
if (let value = self) {
return value;
} else {
panic message;
}
}
Returns the value if it is not nil, otherwise panics with the given message.
[src]
pub fn expect(self, message: string): T {
if (let value = self) {
return value;
} else {
panic message;
}
}
Returns the value if it is not nil, otherwise panics with the given message.
Returns the value if it is not nil, otherwise panics with the given message.
Panics
Panics if the value is a nil
with a custom panic message provided by msg.
Examples
let a: string? = "hello";
let b: string? = nil;
a.expect("a is nil"); // "hello";
b.expect("b is nil"); // panic "b is nil"
unwrap
pub fn unwrap(self): T
[src]
pub fn unwrap(self): T {
if (let value = self) {
return value;
} else {
panic "called `unwrap()` on a `nil` value";
}
}
Returns the value if it is not nil, otherwise panics with a default message.
unwrap
pub fn unwrap(self): T
[src]
pub fn unwrap(self): T {
if (let value = self) {
return value;
} else {
panic "called `unwrap()` on a `nil` value";
}
}
Returns the value if it is not nil, otherwise panics with a default message.
Returns the value if it is not nil, otherwise panics with a default message.
This is same like !
operator.
Panics
Panics if the value is a nil
with a default panic message.
Examples
let a: string? = "hello";
let b: string? = nil;
a.unwrap(); // "hello"
a!; // "hello"
b.unwrap(); // panic "called `unwrap()` on a `nil` value"
b!; // panic "called unwrap on a `nil` value"
unwrap_or
pub fn unwrap_or(self, default_: T): T
[src]
pub fn unwrap_or(self, default_: T): T {
if (let value = self) {
return value;
} else {
return default_;
}
}
Returns the value if it is not nil, otherwise returns the default value.
unwrap_or
pub fn unwrap_or(self, default_: T): T
[src]
pub fn unwrap_or(self, default_: T): T {
if (let value = self) {
return value;
} else {
return default_;
}
}
Returns the value if it is not nil, otherwise returns the default value.
Returns the value if it is not nil, otherwise returns the default value.
The default
argument is must be the same type as the value.
Examples
let a: string? = "hello";
let b: string? = nil;
assert_eq a.unwrap_or("world"), "hello";
assert_eq b.unwrap_or("world"), "world";
or
pub fn or(self, optb: T?): T?
[src]
pub fn or(self, optb: T?): T? {
if (let value = self) {
return value;
} else {
return optb;
}
}
Returns the option if it contains a value, otherwise returns optb
.
or
pub fn or(self, optb: T?): T?
[src]
pub fn or(self, optb: T?): T? {
if (let value = self) {
return value;
} else {
return optb;
}
}
Returns the option if it contains a value, otherwise returns optb
.
Returns the option if it contains a value, otherwise returns optb
.
Examples
let a: string? = "hello";
let b: string? = nil;
let c: string? = nil;
assert_eq a.or(b), "hello";
assert_eq b.or(a), "hello";
assert_eq b.or(c), nil;
or_else
pub fn or_else(self, f: |()|: T?): T?
[src]
pub fn or_else(self, f: |(): T?|): T? {
if (let value = self) {
return value;
} else {
return f();
}
}
Returns the option if it contains a value, otherwise calls f
and returns the result.
or_else
pub fn or_else(self, f: |()|: T?): T?
[src]
pub fn or_else(self, f: |(): T?|): T? {
if (let value = self) {
return value;
} else {
return f();
}
}
Returns the option if it contains a value, otherwise calls f
and returns the result.
Returns the option if it contains a value, otherwise calls f
and returns the result.
Examples
let a: string? = "hello";
let b: string? = nil;
let c: string? = nil;
assert_eq a.or_else(|| "world"), "hello";
assert_eq b.or_else(|| "world"), "world";
assert_eq c.or_else(|| {
let value: string? = nil;
return nil;
}), nil;
and
pub fn and<U>(self, optb: T?): T?
[src]
pub fn and<U>(self, optb: U?): U? {
if (let value = self) {
return optb;
} else {
return nil;
}
}
Returns nil
if the optional value is nil
, otherwise returns optb
.
and
pub fn and<U>(self, optb: T?): T?
[src]
pub fn and<U>(self, optb: U?): U? {
if (let value = self) {
return optb;
} else {
return nil;
}
}
Returns nil
if the optional value is nil
, otherwise returns optb
.
Returns nil
if the optional value is nil
, otherwise returns optb
.
Examples
let a: string? = "hello";
let b: string? = nil;
let c: string? = "c";
assert_eq a.and(c), "c";
assert_eq b.and(c), nil;
and_then
pub fn and_then<Q>(self, f: |(T)|: T?): T?
[src]
pub fn and_then<Q>(self, f: |(T): Q?|): Q? {
if (let value = self) {
return f(value);
} else {
return nil;
}
}
Returns nil
if the optional value is nil
, otherwise calls f
and returns the result.
and_then
pub fn and_then<Q>(self, f: |(T)|: T?): T?
[src]
pub fn and_then<Q>(self, f: |(T): Q?|): Q? {
if (let value = self) {
return f(value);
} else {
return nil;
}
}
Returns nil
if the optional value is nil
, otherwise calls f
and returns the result.
Returns nil
if the optional value is nil
, otherwise calls f
and returns the result.
Examples
let a: string? = "hello";
let b: string? = nil;
let c: int? = 123;
assert_eq a.and_then(|s| `${s} world`), "hello world";
assert_eq b.and_then(|s| `${s} world`), nil;
assert_eq c.and_then(|v| `${v}`), "123";
map
pub fn map<U>(self, f: |(T)|: T): T?
[src]
pub fn map<U>(self, f: |(T): U|): U? {
if (let value = self) {
return f(value);
} else {
return nil;
}
}
Maps an optional value to another value by applying a function to it.
map
pub fn map<U>(self, f: |(T)|: T): T?
[src]
pub fn map<U>(self, f: |(T): U|): U? {
if (let value = self) {
return f(value);
} else {
return nil;
}
}
Maps an optional value to another value by applying a function to it.
Maps an optional value to another value by applying a function to it.
If the value is nil
, the function will not be called and the result will be nil
.
Examples
let a: string? = "hello";
let b: string? = nil;
assert_eq a.map::<string, int>(|s| s.len()), 5;
assert_eq a.map::<string, string>(|s| `a = ${s}`), "a = hello";
assert_eq b.map::<string, int>(|s| s.len()), nil;
map_or
pub fn map_or<U>(self, default_: T, f: |(T)|: T): T
[src]
pub fn map_or<U>(self, default_: U, f: |(T): U|): U {
if (let value = self) {
return f(value);
} else {
return default_;
}
}
Maps an optional value to another value by applying a function to it.
map_or
pub fn map_or<U>(self, default_: T, f: |(T)|: T): T
[src]
pub fn map_or<U>(self, default_: U, f: |(T): U|): U {
if (let value = self) {
return f(value);
} else {
return default_;
}
}
Maps an optional value to another value by applying a function to it.
Maps an optional value to another value by applying a function to it.
If the value is nil
, the function will not be called and the result will be nil
.
Examples
let a: string? = "hello";
let b: string? = nil;
assert_eq a.map_or::<string, int>(0, |s| s.len()), 5;
assert_eq b.map_or::<string, int>(0, |s| s.len()), 0;
inspect
pub fn inspect(self, f: |(T)|): T?
[src]
pub fn inspect(self, f: |(T)|): T? {
if (let value = self) {
f(value);
return value;
} else {
return nil;
}
}
Calls the provided closure to the contained value (if not nil
).
inspect
pub fn inspect(self, f: |(T)|): T?
[src]
pub fn inspect(self, f: |(T)|): T? {
if (let value = self) {
f(value);
return value;
} else {
return nil;
}
}
Calls the provided closure to the contained value (if not nil
).
Calls the provided closure to the contained value (if not nil
).
Examples
use std.io;
let a: string? = "hello";
let b: string? = nil;
// prints "got: hello"
assert_eq a.inspect(|s| {
io.println(`got: ${s}`);
}), "hello";
// prints nothing
assert_eq b.inspect(|s| {
io.println(`got: ${s}`);
}), nil;
ok_or
pub fn ok_or<E>(self, err: T): T throws T
[src]
pub fn ok_or<E: Error>(self, err: E): T throws E {
if (let value = self) {
return value;
} else {
throw err;
}
}
Returns the inner value if it contains a value, otherwise throws the error err
.
ok_or
pub fn ok_or<E>(self, err: T): T throws T
[src]
pub fn ok_or<E: Error>(self, err: E): T throws E {
if (let value = self) {
return value;
} else {
throw err;
}
}
Returns the inner value if it contains a value, otherwise throws the error err
.
Returns the inner value if it contains a value, otherwise throws the error err
.
Examples
use std.testing.assert_throws;
let a: string? = "hello";
let b: string? = nil;
assert_eq try! a.ok_or("world"), "hello";
assert_throws(|| {
try b.ok_or("this is error");
}, "this is error");
ok_or_else
pub fn ok_or_else<E>(self, f: |()|: T): T throws T
[src]
pub fn ok_or_else<E: Error>(self, f: |(): E|): T throws E {
if (let value = self) {
return value;
} else {
throw f();
}
}
Returns the inner value if it contains a value, otherwise call f
and throws the error err
.
ok_or_else
pub fn ok_or_else<E>(self, f: |()|: T): T throws T
[src]
pub fn ok_or_else<E: Error>(self, f: |(): E|): T throws E {
if (let value = self) {
return value;
} else {
throw f();
}
}
Returns the inner value if it contains a value, otherwise call f
and throws the error err
.
Returns the inner value if it contains a value, otherwise call f
and throws the error err
.
Examples
use std.testing.assert_throws;
let a: string? = "hello";
let b: string? = nil;
assert_eq try! a.ok_or_else(|| "world"), "hello";
assert_throws(|| {
try b.ok_or_else(|| "this is error");
}, "this is error");
Returns true
if the optional value is not nil
, otherwise false
.
Returns true
if the optional value is not nil
, otherwise false
.
Returns true
if the optional value is not nil
, otherwise false
.
Examples
let a: string? = "hello";
let b: string? = nil;
assert a.is_some();
assert !b.is_some();