I'm trying a simple code, in which I've 2 elements of vec type (or or 1s array type) bith elements are of the same size, and I just want to add the items with the same index together.
I wanted to try doing it using functional programming like x.iter().map(|v| ...) but did not know how to do it, so tried doing it the classical way, and wrote the below function, but got stuck with borrowing and referencing and got errors:
Tried the below code:
fn foo(x: &[i32], y: &[i32]) -> &'static [i32] {
let r: &'static [i32];
for i in x {
r[i] = x[i] * y[i];
}
// functional approach failed: r = x.iter().map(|v| *v) * y.iter().map(|v| *v).collect();
r
}
Got the below errors:
error[E0277]: the type `[i32]` cannot be indexed by `&i32`
--> src\main.rs:42:13
|
42 | r[i] = x[i] * y[i];
| ^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `std::slice::SliceIndex<[i32]>` is not implemented for `&i32`
= note: required because of the requirements on the impl of `std::ops::Index<&i32>` for `[i32]`
error[E0277]: the type `[i32]` cannot be indexed by `&i32`
--> src\main.rs:42:20
|
42 | r[i] = x[i] * y[i];
| ^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `std::slice::SliceIndex<[i32]>` is not implemented for `&i32`
= note: required because of the requirements on the impl of `std::ops::Index<&i32>` for `[i32]`
error[E0277]: the type `[i32]` cannot be indexed by `&i32`
--> src\main.rs:42:27
|
42 | r[i] = x[i] * y[i];
| ^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `std::slice::SliceIndex<[i32]>` is not implemented for `&i32`
= note: required because of the requirements on the impl of `std::ops::Index<&i32>` for `[i32]`
error: aborting due to 3 previous errors
Thanks, this solved my issue, but what if I want to work with arrays, for doing something more complicated, like working with multidimensional arrays, and return multidimensional array?
Array multiplication is a bit complicated because you have to iterate over strides or transpose the second matrix which will need to allocate. There isn't a way to easily do it from std.
type OryxResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
fn foo(x: &[&Vec<f64>], y: &[&Vec<f64>]) -> OryxResult<Vec<f64>> {
// n x m * m x p = n x p
let m = x.len(); // 2 // number of vecs
let n = x[0].len(); // 3 // vec length
let _m = y[0].len(); // 2 // vec length
let p = y.len(); // 1 // number of vecs
if x.len() != y[0].len() {
Err(format!("cross dimentional is invalid"))?
} else {
//let mut array: Vec<Vec<i32>> = vec![vec![Default::default(); p]; n];
let mut array: Vec<f64> = vec![Default::default(); n];
for i in 0..n { // 3;
for j in 0..m { // 2
for k in 0..p { // 1
// array[i][k] += x[j][i] * y[k][j];
array[i] += x[j][i] * y[k][j];
}
}
}
Ok(array)
}
}