Rust ndarray: how to pass a slice as pointer array (&[T]) to function and mutable pointer array (&mut [T])?

261 Views Asked by At

I have a 3-dimensional array Array3 of the ndarray crate, and I would like to pass a slice of it as an array (&[T]) to a function, but can't figure out how. I found slice() and as_slice() methods, among similar others, but they require the data to be contiguous, or they return None instead. For instance, something like this:

fn foo<T>(a: &[T], b: &mut [T]) {
    // do stuff
}

fn main() {

    let a = array![
        [
            [1.55, 0.0, 0.45, 0.0, 0.25], 
            [1.0, 0.55, 0.45, 0.0, 0.25], 
            [1.0, 0.55, 0.0, 0.0, 0.25],
        ],
        [
            [1.0, 0.0, 0.45, 0.65, 0.25], 
            [1.0, 0.55, 0.45, 0.65, 0.25], 
            [1.0, 0.55, 0.0, 0.65, 0.25],
        ],
        [
            [1.0, 0.0, 0.45, 0.65, 0.0], 
            [1.0, 0.55, 0.45, 0.65, 0.0], 
            [1.45, 0.55, 0.0, 0.65, 0.0],
        ],
    ];

    let mut b = array![
        [1.0, 0.0, 0.0],
        [1.0, 0.0, 0.0],
        [1.0, 0.0, 0.0],
    ];

    foo(
        a.slice(s![.., 0, 0]).as_slice().unwrap(), 
        b.slice_mut(s![.., 0]).as_slice_mut().unwrap(),
    );
}

How can I pass these slices in order to treat them as an array and mutable array? From the documentation, I am having difficulty finding out.

0

There are 0 best solutions below