Reputation: 3028
I am using ndarray
and trying to slice some arrays. This works
let y = arr2(&[[ 6, 5, 4],
[12, 11, 10]]);
let ip = y.slice(s![0, ..]);
println!("IP {}", ip);
but this
let y = arr2(&[[ 6, 5, 4],
[12, 11, 10]]);
let ip = y.slice(s![0, ..]);
println!("IP {}", ip[0]);
does not compile. What is going on?
Compile errors are:
Error[E0277]: the trait bound `i32: Dimension` is not satisfied
....
println!("IP {}", ip[0]);
| ^^^^^ the trait `Dimension` is not implemented for `i32`
| = note: required because of the requirements on the impl of `std::ops::Index<i32>` for `ArrayBase<ViewRepr<&i32>, i32>`
with other errors
error[E0308]: mismatched types
....
| let ip = y.slice(s![0, ..]);
| ^^^^^^^^^^^^^^^^^^ expected `i32`, found struct `Dim`
|
= note: expected type `i32`
found struct `Dim<[usize; 1]>`
Upvotes: 0
Views: 1027
Reputation: 13790
The issue is that integer literals are by default i32
, and so ip[0]
tells Rust that ip: Index<i32>
, which then fixes the type of slice
's argument to be i32
, but the type is supposed to be impl Dimension
. (I don't fully understand the type hierarchy of ndarray
's indices/dimensions, but this is close enough to be useful.) To work around this, simple replace ip[0]
with ip[0_usize]
.
Upvotes: 0