Question 2: are there any libraries for multi-dimensional arrays? I have seen comments about such things but not any real code, yet.
What I’d prefer not to use is Vec<Vec<Vec<f64>>>, but at the moment it might be either that or a crude wrapper holding strides and un-sugared index function.
The code above is pretty slow with the current rustc though. With unsafe code it is 9× faster:
pub fn make_arr_unsafe(len: usize) -> Box<[i32]> {
use std::rt::heap::allocate;
use std::mem::{min_align_of, transmute};
use std::ptr::set_memory;
use std::raw::Slice;
use std::i32;
let size = len * i32::BYTES;
unsafe {
let mem = allocate(size, min_align_of::<i32>());
set_memory(mem, 0, size);
let slice = Slice { data: mem as *const i32, len: len };
transmute(slice)
}
}
I’ve written rust-ndarray (docs) but mostly to learn how numpy’s ndarray works! It’s a real multidimensional array. The most useful part of it (for me) is multidimensional slices. I would be happy to hear feedback, and be wary, I’m sure it is not well optimized yet.
Well the dimension is fixed by the type, it’s in the second type parameter. Array2 would be Array<A, (Ix, Ix)>. You’re free to type alias this in your own code, I think that’s the best anyway.