Enum solana_sdk::program_option::COption[][src]

#[repr(C)]
pub enum COption<T> {
    None,
    Some(T),
}
Expand description

A C representation of Rust’s std::option::Option

Variants

None

No value

Some(T)

Some value T

Implementations

Returns true if the option is a COption::Some value.

Examples

let x: COption<u32> = COption::Some(2);
assert_eq!(x.is_some(), true);

let x: COption<u32> = COption::None;
assert_eq!(x.is_some(), false);

Returns true if the option is a COption::None value.

Examples

let x: COption<u32> = COption::Some(2);
assert_eq!(x.is_none(), false);

let x: COption<u32> = COption::None;
assert_eq!(x.is_none(), true);

Returns true if the option is a COption::Some value containing the given value.

Examples

#![feature(option_result_contains)]

let x: COption<u32> = COption::Some(2);
assert_eq!(x.contains(&2), true);

let x: COption<u32> = COption::Some(3);
assert_eq!(x.contains(&2), false);

let x: COption<u32> = COption::None;
assert_eq!(x.contains(&2), false);

Converts from &COption<T> to COption<&T>.

Examples

Converts an COption<String> into an COption<usize>, preserving the original. The map method takes the self argument by value, consuming the original, so this technique uses as_ref to first take an COption to a reference to the value inside the original.

let text: COption<String> = COption::Some("Hello, world!".to_string());
// First, cast `COption<String>` to `COption<&String>` with `as_ref`,
// then consume *that* with `map`, leaving `text` on the stack.
let text_length: COption<usize> = text.as_ref().map(|s| s.len());
println!("still can print text: {:?}", text);

Converts from &mut COption<T> to COption<&mut T>.

Examples

let mut x = COption::Some(2);
match x.as_mut() {
    COption::Some(v) => *v = 42,
    COption::None => {},
}
assert_eq!(x, COption::Some(42));

Unwraps an option, yielding the content of a COption::Some.

Panics

Panics if the value is a COption::None with a custom panic message provided by msg.

Examples

let x = COption::Some("value");
assert_eq!(x.expect("the world is ending"), "value");
let x: COption<&str> = COption::None;
x.expect("the world is ending"); // panics with `the world is ending`

Moves the value v out of the COption<T> if it is COption::Some(v).

In general, because this function may panic, its use is discouraged. Instead, prefer to use pattern matching and handle the COption::None case explicitly.

Panics

Panics if the self value equals COption::None.

Examples

let x = COption::Some("air");
assert_eq!(x.unwrap(), "air");
let x: COption<&str> = COption::None;
assert_eq!(x.unwrap(), "air"); // fails

Returns the contained value or a default.

Arguments passed to unwrap_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use unwrap_or_else, which is lazily evaluated.

Examples

assert_eq!(COption::Some("car").unwrap_or("bike"), "car");
assert_eq!(COption::None.unwrap_or("bike"), "bike");

Returns the contained value or computes it from a closure.

Examples

let k = 10;
assert_eq!(COption::Some(4).unwrap_or_else(|| 2 * k), 4);
assert_eq!(COption::None.unwrap_or_else(|| 2 * k), 20);

Maps an COption<T> to COption<U> by applying a function to a contained value.

Examples

Converts an COption<String> into an COption<usize>, consuming the original:

let maybe_some_string = COption::Some(String::from("Hello, World!"));
// `COption::map` takes self *by value*, consuming `maybe_some_string`
let maybe_some_len = maybe_some_string.map(|s| s.len());

assert_eq!(maybe_some_len, COption::Some(13));

Applies a function to the contained value (if any), or returns the provided default (if not).

Examples

let x = COption::Some("foo");
assert_eq!(x.map_or(42, |v| v.len()), 3);

let x: COption<&str> = COption::None;
assert_eq!(x.map_or(42, |v| v.len()), 42);

Applies a function to the contained value (if any), or computes a default (if not).

Examples

let k = 21;

let x = COption::Some("foo");
assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 3);

let x: COption<&str> = COption::None;
assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 42);

Transforms the COption<T> into a Result<T, E>, mapping COption::Some(v) to Ok(v) and COption::None to Err(err).

Arguments passed to ok_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use ok_or_else, which is lazily evaluated.

Examples

let x = COption::Some("foo");
assert_eq!(x.ok_or(0), Ok("foo"));

let x: COption<&str> = COption::None;
assert_eq!(x.ok_or(0), Err(0));

Transforms the COption<T> into a Result<T, E>, mapping COption::Some(v) to Ok(v) and COption::None to Err(err()).

Examples

let x = COption::Some("foo");
assert_eq!(x.ok_or_else(|| 0), Ok("foo"));

let x: COption<&str> = COption::None;
assert_eq!(x.ok_or_else(|| 0), Err(0));

Returns COption::None if the option is COption::None, otherwise returns optb.

Examples

let x = COption::Some(2);
let y: COption<&str> = COption::None;
assert_eq!(x.and(y), COption::None);

let x: COption<u32> = COption::None;
let y = COption::Some("foo");
assert_eq!(x.and(y), COption::None);

let x = COption::Some(2);
let y = COption::Some("foo");
assert_eq!(x.and(y), COption::Some("foo"));

let x: COption<u32> = COption::None;
let y: COption<&str> = COption::None;
assert_eq!(x.and(y), COption::None);

Returns COption::None if the option is COption::None, otherwise calls f with the wrapped value and returns the result.

COption::Some languages call this operation flatmap.

Examples

fn sq(x: u32) -> COption<u32> { COption::Some(x * x) }
fn nope(_: u32) -> COption<u32> { COption::None }

assert_eq!(COption::Some(2).and_then(sq).and_then(sq), COption::Some(16));
assert_eq!(COption::Some(2).and_then(sq).and_then(nope), COption::None);
assert_eq!(COption::Some(2).and_then(nope).and_then(sq), COption::None);
assert_eq!(COption::None.and_then(sq).and_then(sq), COption::None);

Returns COption::None if the option is COption::None, otherwise calls predicate with the wrapped value and returns:

This function works similar to Iterator::filter(). You can imagine the COption<T> being an iterator over one or zero elements. filter() lets you decide which elements to keep.

Examples

fn is_even(n: &i32) -> bool {
    n % 2 == 0
}

assert_eq!(COption::None.filter(is_even), COption::None);
assert_eq!(COption::Some(3).filter(is_even), COption::None);
assert_eq!(COption::Some(4).filter(is_even), COption::Some(4));

Returns the option if it contains a value, otherwise returns optb.

Arguments passed to or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use or_else, which is lazily evaluated.

Examples

let x = COption::Some(2);
let y = COption::None;
assert_eq!(x.or(y), COption::Some(2));

let x = COption::None;
let y = COption::Some(100);
assert_eq!(x.or(y), COption::Some(100));

let x = COption::Some(2);
let y = COption::Some(100);
assert_eq!(x.or(y), COption::Some(2));

let x: COption<u32> = COption::None;
let y = COption::None;
assert_eq!(x.or(y), COption::None);

Returns the option if it contains a value, otherwise calls f and returns the result.

Examples

fn nobody() -> COption<&'static str> { COption::None }
fn vikings() -> COption<&'static str> { COption::Some("vikings") }

assert_eq!(COption::Some("barbarians").or_else(vikings), COption::Some("barbarians"));
assert_eq!(COption::None.or_else(vikings), COption::Some("vikings"));
assert_eq!(COption::None.or_else(nobody), COption::None);

Returns COption::Some if exactly one of self, optb is COption::Some, otherwise returns COption::None.

Examples

let x = COption::Some(2);
let y: COption<u32> = COption::None;
assert_eq!(x.xor(y), COption::Some(2));

let x: COption<u32> = COption::None;
let y = COption::Some(2);
assert_eq!(x.xor(y), COption::Some(2));

let x = COption::Some(2);
let y = COption::Some(2);
assert_eq!(x.xor(y), COption::None);

let x: COption<u32> = COption::None;
let y: COption<u32> = COption::None;
assert_eq!(x.xor(y), COption::None);

Inserts v into the option if it is COption::None, then returns a mutable reference to the contained value.

Examples

let mut x = COption::None;

{
    let y: &mut u32 = x.get_or_insert(5);
    assert_eq!(y, &5);

    *y = 7;
}

assert_eq!(x, COption::Some(7));

Inserts a value computed from f into the option if it is COption::None, then returns a mutable reference to the contained value.

Examples

let mut x = COption::None;

{
    let y: &mut u32 = x.get_or_insert_with(|| 5);
    assert_eq!(y, &5);

    *y = 7;
}

assert_eq!(x, COption::Some(7));

Replaces the actual value in the option by the value given in parameter, returning the old value if present, leaving a COption::Some in its place without deinitializing either one.

Examples

let mut x = COption::Some(2);
let old = x.replace(5);
assert_eq!(x, COption::Some(5));
assert_eq!(old, COption::Some(2));

let mut x = COption::None;
let old = x.replace(3);
assert_eq!(x, COption::Some(3));
assert_eq!(old, COption::None);

Maps an COption<&T> to an COption<T> by copying the contents of the option.

Examples

let x = 12;
let opt_x = COption::Some(&x);
assert_eq!(opt_x, COption::Some(&12));
let copied = opt_x.copied();
assert_eq!(copied, COption::Some(12));

Maps an COption<&mut T> to an COption<T> by copying the contents of the option.

Examples

let mut x = 12;
let opt_x = COption::Some(&mut x);
assert_eq!(opt_x, COption::Some(&mut 12));
let copied = opt_x.copied();
assert_eq!(copied, COption::Some(12));

Maps an COption<&T> to an COption<T> by cloning the contents of the option.

Examples

let x = 12;
let opt_x = COption::Some(&x);
assert_eq!(opt_x, COption::Some(&12));
let cloned = opt_x.cloned();
assert_eq!(cloned, COption::Some(12));

Maps an COption<&mut T> to an COption<T> by cloning the contents of the option.

Examples

let mut x = 12;
let opt_x = COption::Some(&mut x);
assert_eq!(opt_x, COption::Some(&mut 12));
let cloned = opt_x.cloned();
assert_eq!(cloned, COption::Some(12));

Returns the contained value or a default

Consumes the self argument then, if COption::Some, returns the contained value, otherwise if COption::None, returns the default value for that type.

Examples

Converts a string to an integer, turning poorly-formed strings into 0 (the default value for integers). parse converts a string to any other type that implements FromStr, returning COption::None on error.

let good_year_from_input = "1909";
let bad_year_from_input = "190blarg";
let good_year = good_year_from_input.parse().ok().unwrap_or_default();
let bad_year = bad_year_from_input.parse().ok().unwrap_or_default();

assert_eq!(1909, good_year);
assert_eq!(0, bad_year);

Converts from COption<T> (or &COption<T>) to COption<&T::Target>.

Leaves the original COption in-place, creating a new one with a reference to the original one, additionally coercing the contents via Deref.

Examples

#![feature(inner_deref)]

let x: COption<String> = COption::Some("hey".to_owned());
assert_eq!(x.as_deref(), COption::Some("hey"));

let x: COption<String> = COption::None;
assert_eq!(x.as_deref(), COption::None);

Converts from COption<T> (or &mut COption<T>) to COption<&mut T::Target>.

Leaves the original COption in-place, creating a new one containing a mutable reference to the inner type’s Deref::Target type.

Examples

#![feature(inner_deref)]

let mut x: COption<String> = COption::Some("hey".to_owned());
assert_eq!(x.as_deref_mut().map(|x| {
    x.make_ascii_uppercase();
    x
}), COption::Some("HEY".to_owned().as_mut_str()));

Transposes an COption of a Result into a Result of an COption.

COption::None will be mapped to Ok(COption::None). COption::Some(Ok(_)) and COption::Some(Err(_)) will be mapped to Ok(COption::Some(_)) and Err(_).

Examples

#[derive(Debug, Eq, PartialEq)]
struct COption::SomeErr;

let x: Result<COption<i32>, COption::SomeErr> = Ok(COption::Some(5));
let y: COption<Result<i32, COption::SomeErr>> = COption::Some(Ok(5));
assert_eq!(x, y.transpose());

Converts from COption<COption<T>> to COption<T>

Examples

Basic usage:

#![feature(option_flattening)]
let x: COption<COption<u32>> = COption::Some(COption::Some(6));
assert_eq!(COption::Some(6), x.flatten());

let x: COption<COption<u32>> = COption::Some(COption::None);
assert_eq!(COption::None, x.flatten());

let x: COption<COption<u32>> = COption::None;
assert_eq!(COption::None, x.flatten());

Flattening once only removes one level of nesting:

#![feature(option_flattening)]
let x: COption<COption<COption<u32>>> = COption::Some(COption::Some(COption::Some(6)));
assert_eq!(COption::Some(COption::Some(6)), x.flatten());
assert_eq!(COption::Some(6), x.flatten().flatten());

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns [COption::None][COption::COption::None].

Examples

let opt: COption<u32> = COption::default();
assert!(opt.is_none());

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.