Pass [[f64]] reference to function

Hello everyone,

Is it possible to passe a [[f64]] by reference to a function ? For example :

const N: usize = 100;

let a: [[f64;N];N] = [[0.0;N];N];

my_function(&a)

I know this code will not compile, but it exists a way to do the same efficiently ?

I have the same question to a [[[f64]]] array.

Just another question : is it true that vector has less fast as array in Rust ?

Thank you !

No you cannot

Yes, vectors are slower than arrays, but you will likely never see them in your profiler, so I wouldn't worry about it.

Also Rust doesn't support arrays thar well yet, so it will be much easier to use vectors.

Okay, thank you !

right, for

fn my_function(a: &[[f64;N];N]) {
}

it does need to know N in advance, there's currently no way to parameterize the function type over different array sizes, this would need const generics

&[[f64;N];N] doesn't automatically coerce to a slice of slices [[f64]] because it has a very different representation, if you have

fn my_function(a: &[&[f64]]) {
}

then to call it, you'd need to generate an vector (or array) of slices first

fn main() {
    let a: [[f64;N];N] = [[0.0;N];N];
    let rows = a.iter().map(|x| &x[..]).collect::<Vec<_>>();
    my_function(&rows);
}

not with regard to access, as far as i know—both an array and a vector lay out the elements consecutively in memory—the difference in performance is that a Vector requires a separate heap allocation to create, whereas arrays are allocated on the stack (or as part of the structure they're part of)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.