简体   繁体   中英

How to return an array in Rust function

I wanted to create a function in Rust that generates an Array of x size with random values. I would like to ask how to return an array in Rust function and it would be nice if you can check if the rest of my code is ok. I am sorry for my basic question but I am a beginner.

use rand::prelude::*;

fn generateArray(howManyValues: u32)->[f64]
{
    let mut rng = rand::thread_rng();
    let array: [f64, howManyValues];
    for i in 0..howManyValues
    {
        array[i] = rng.gen()*100;
    }

    println!("{:?}", array);
    return array;
}

fn main() {
    generateArray(10);
}

I want to create a function in Rust 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] [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. But if it is too big to fit on the stack, you'll get a stack overflow.

use rand::prelude::*;

const CONST_LEN: usize = 10;

fn generate_array() -> [f64; CONST_LEN] {
    let mut rng = rand::thread_rng();
    let mut arr = [0.; CONST_LEN];
    for item in arr.iter_mut() {
        *item = rng.gen::<f64>() * 100.;
    }
    arr
}

fn main() {
    let generated_array = generate_array();
    println!("{:?}", generated_array);
}

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.

First like other programming languages array in Rust cannot grow or shrink. You can either use a vector.

let v : Vec<i32> = Vec::new();

you can use .push() function to push values in vector.

Adapting your function, and still returning an array, requires specifying the number of elements in the return type ie fn generateArray()->[f64; 100] fn generateArray()->[f64; 100] . The rust type system accounts for the size of an array, so [f64; 100] [f64; 100] (an array of length 100 containing f64 s) is a different type from [f64; 99] [f64; 99] . Specifying the array's length in the function makes is less reusable, as it always returns the same size array.

If you simply must create stack-allocated arrays full of random floats, you could use a macro, but this limits you to array sizes known at compile time ( constexpr in C++ lingo, const in Rust lingo). I consider this macro approach slightly unidiomatic rust. The macro could look like

use rand::prelude::*;

macro_rules! make_array {
    ($size:literal) => {{
        let mut rng = rand::thread_rng();
        let mut arr = [0_f64; $size];
        for item in arr.iter_mut() {
            *item = rng.gen::<f64>()*100.;
        }
        arr
    }}
}

fn main() {
    let arr = make_array!(32);
    println!("{:?}", arr);
    let arr = make_array!(16);
    println!("{:?}", arr);
}

This is more reusable than the modified function, as you can specify different numbers of elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM