[][src]Trait higher_order_functions::Init

pub trait Init<T, I, V = ()>: Sized {
    fn init_with<F: FnMut(I) -> T>(value: V, elem: F) -> Self;

    fn init<F: FnMut(I) -> T>(elem: F) -> Self
    where
        V: TypeEquals<()>
, { ... } }

Types which can be initialised by applying a function to each 'index' of the type.

Examples

use higher_order_functions::Init;

struct House { number: usize }

// [T; N]: Init<T, usize>
let road = <[House; 3]>::init(|i| House { number: i + 1 });

assert_eq!(road[0].number, 1);
assert_eq!(road[1].number, 2);
assert_eq!(road[2].number, 3);

Required methods

fn init_with<F: FnMut(I) -> T>(value: V, elem: F) -> Self

Initialise an instance of this type using value by applying elem to each 'index' of the type.

Examples

Constructing a Vec containing the values 0 to 4:

use higher_order_functions::Init;
	
let vec = Vec::<usize>::init_with(5, |i| i);
	
assert_eq!(vec, vec![0, 1, 2, 3, 4]);
Loading content...

Provided methods

fn init<F: FnMut(I) -> T>(elem: F) -> Self where
    V: TypeEquals<()>, 

Initialise an instance of this type by applying elem to each 'index' of the type.

This is syntax sugar for init_with((), elem).

Examples

Constructing an array containing the values 0 to 4:

use higher_order_functions::Init;

let arr = <[usize; 5]>::init(|i| i);

assert_eq!(arr, [0, 1, 2, 3, 4]);
Loading content...

Implementations on Foreign Types

impl<T, const N: usize> Init<T, usize, ()> for [T; N][src]

Initialise an array from a function applied to each index.

impl<T> Init<T, usize, usize> for Vec<T>[src]

Initialise a Vec from a length and a function applied to each index.

impl<T> Init<T, usize, usize> for LinkedList<T>[src]

Initialise a LinkedList from a length and a function applied to each index.

impl<T> Init<Option<T>, usize, ()> for LinkedList<T>[src]

Initialise a LinkedList from a function called repeatedly until it returns None.

This is equivalent to an unfold but with a mutable closure instead of a mutable state parameter.

Loading content...

Implementors

Loading content...