I want to create a function that generates an array of X size with random values.
The type [f64] is a slice, not an array. An array needs a length, like [f64; 25].
Rust's Vec is probably better than a array or slice for this job, unless you're using some sort of foreign function interface (FFI), or a Rust API requires an array (it probably requires Deref<Target = [T]>, basically generic over all array-like types including Vec and arrays). Vec is analogous to C++'s std::vector, and Java's ArrayList.
use rand::prelude::*;
fn generate_vec(len: usize) -> Vec<f64> {
let mut rng = rand::thread_rng();
let mut vec = Vec::with_capacity(len);
for _ in 0..len {
vec.push(rng.gen::<f64>() * 100.);
}
return vec;
}
fn main() {
let generated_vec = generate_vec(10);
println!("{:?}", generated_vec);
}
I made some style changes[1]. If you really want an array, read on.
If you know the size of the array at compile time, you can use an array. If it is too big to fit on the stack, you'll get a stack overflow. Rust 1.51 made this slightly more ergonomic with const generics.
use rand::prelude::*;
fn generate_array<const LEN: usize>() -> [f64; LEN] {
let mut rng = rand::thread_rng();
let mut arr = [0.; LEN];
for item in arr.iter_mut() {
*item = rng.gen::<f64>() * 100.;
}
arr
}
fn main() {
// generate_array can make an array of any length (within stack size limits)
let generated_array1:[f64; 5] = generate_array();
let generated_array2:[f64; 10] = generate_array();
println!("{:?}", generated_array2);
}
If you don't know the length at compile time, or it is too big to fit on the stack, the closest you can get to an array is Box<[T]> (Box of slice of T) using into_boxed_slice().
use rand::prelude::*;
fn generate_array(len: usize) -> Box<[f64]> {
let mut rng = rand::thread_rng();
let mut vec = Vec::with_capacity(len);
for _ in 0..len {
vec.push(rng.gen::<f64>() * 100.);
}
vec.into_boxed_slice()
}
fn main() {
let generated_array = generate_array(10);
println!("{:?}", generated_array);
}
[1] words in function names separated with underscores instead of camelCase, moved println to main() and changed the type of len to usize, since this is how arrays are typically indexed in Rust.