Thanks, this solved the issue of step by
, so that I can write:
let alpha: Vec<f64> = (0..=100).map(|n| n as f64 / 100.0).collect();
let beta: Vec<f64> = (0..=100).map(|n| n as f64 / 100.0).collect();
let gamma: Vec<f64> = (0..=100).map(|n| n as f64 / 100.0).collect();
Now the issue of all combinations, to simplify it, let's say we have:
let alpha = vec![1,2];
let beta = vec![4,8];
let gamma = vec![3,7];
Then we are having 8 combinations as:
[1, 4, 3]
[1, 4, 7]
[1, 8, 3]
[1, 8, 7]
[2, 4, 3]
[2, 4, 7]
[2, 8, 3]
[2, 8, 7]
How can I get this output with a rust code?
I was able to doit with the below code, but is this the way, or there is a better way:
fn main() {
let alpha = vec![1,2];
let beta = vec![4,8];
let gamma = vec![3,7];
let mut all: Vec<[&i32; 3]> = Vec::new();
for i in &alpha {
for j in &beta {
for k in &gamma {
all.push([i, j, k]);
}
}
}
println!("{:?}", all);
}
Accordingly the answer of my question is, but is this the best code I can write for replacing the single line Julia
code:
fn main() {
let alpha: Vec<f64> = (0..=100).map(|n| n as f64 / 100.0).collect();
let beta: Vec<f64> = (0..=100).map(|n| n as f64 / 100.0).collect();
let gamma: Vec<f64> = (0..=100).map(|n| n as f64 / 100.0).collect();
let mut all: Vec<[&f64; 3]> = Vec::new();
for i in &alpha {
for j in &beta {
for k in &gamma {
all.push([i, j, k]);
}
}
}
println!("{:?}", all.len());
}