How do I pass an `ndarray` into another function?

I'm trying to pass an ndarray into another function but I'm unsure what type I should give. The docs say that the equivalent of Numpy's np.ndarray type is the ArrayBase<S, D> type, but when I try to use that in the function signature I'm unable to specify the dimension. Would appreciate any help.

Code -

use rand::Rng;
use ndarray::prelude::*;

fn takes_array(&mut lattice: ArrayBase<i32, 2>){
}

fn main() {
    let mut rng = rand::thread_rng();
    let mut lattice = Array::from_shape_fn((100, 100), |x| [-1, 1][rng.gen_range(0..1)]);
}

Error -

   Compiling metropolis v0.1.0 (/home/cocoafedora/Documents/Code/RustLearning/metropolis)
error[E0747]: constant provided when a type was expected
 --> src/main.rs:4:45
  |
4 | fn takes_array(&mut lattice: ArrayBase<i32, 2>){
  |                                             ^

For more information about this error, try `rustc --explain E0747`.
error: could not compile `metropolis` due to previous error

The D parameter, as the error message clearly states, is a type, not a const. See the indexing and dimensionality docs and some library-provided typedefs for more information.

Thanks for the link! I used the example of fill_lower in the link you shared, ran into another issue, and googling that led me to this post which has solved the issue. I was unsure what the type is supposed to be exactly, because lattice has some type like ArrayBase<OwnedRepr<i32>... which was a bit confusing to me so the example helped.

ArrayBase<OwnedRepr<T>, D> is just Array<T, D>.

Yeah I see that now, I didn't realize it before. Very new to lower level languages so just getting my bearings in. Appreciate the help! <3

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.