简体   繁体   中英

How to use another array's length to initialize an array in Rust?

I want to initialize an array whose length is equal to another array's length:

fn foo(array: &[i32]) {
    let mut sum = [0; array.len()];
}

It will make an error:

error[E0080]: constant evaluation error
  --> test.rs:22:18
   |
22 |    let mut sum = [0; array.len()];
   |                      ^^^^^^^^^^^ unsupported constant expr

I think I must use this len() argument... How can I solve this?

You are actually taking the length of a slice, not an array. Array lengths must be known at compile time. array.len() , being the length of a slice, is potentially derived from runtime input, and as such cannot be used as an array length. You can create a vector instead. For example:

use std::iter::repeat;

fn foo(slice: &[i32]){
    let mut sum: Vec<i32> = repeat(0).take(slice.len()).collect();
    // ......
}

To answer the question asked:

How to use another array's length to initialize an array in Rust?

As of Rust 1.51, you can write generic code like:

fn foo<const N: usize>(array: [u8; N]) -> [u8; N] {
    unimplemented!()
}

See Is it possible to control the size of an array using the type parameter of a generic? for further details.

The solution for previous versions of Rust involves a combination of traits and macros. This has the distinct disadvantage of being limited to a set of types. This is discussed further in questions like Why does println! work only for arrays with a length less than 33?


Showcasing the shorter, more idiomatic version of the Vec -based code:

fn foo(slice: &[i32]) {
    let mut sum = vec![0; slice.len()];
}

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