0

I am trying to create an array of the same length as a vector, in rust but I keep getting errors.

//my code
fn main() {
    let v = vec![1,2,3];
    let len = v.len();
    let a: [i32; len] = [0; len];
    println!("{:?}", a);
}

The Error Message :

   Compiling playground v0.0.1 (/playground)
error[E0435]: attempt to use a non-constant value in a constant
 --> src/main.rs:4:18
  |
3 |     let len = v.len();
  |     ------- help: consider using `const` instead of `let`: `const len`
4 |     let a: [i32; len] = [0; len];
  |                  ^^^ non-constant value

error[E0435]: attempt to use a non-constant value in a constant
 --> src/main.rs:4:29
  |
3 |     let len = v.len();
  |     ------- help: consider using `const` instead of `let`: `const len`
4 |     let a: [i32; len] = [0; len];
  |                             ^^^ non-constant value

For more information about this error, try `rustc --explain E0435`.
error: could not compile `playground` due to 2 previous errors

If I declare len as a const

fn main() {
    let v = vec![1,2,3];
    const len: usize = v.len();
    let a: [i32; len] = [0; len];
    println!("{:?}", a);
}

I get this error:

  Compiling playground v0.0.1 (/playground)
error[E0435]: attempt to use a non-constant value in a constant
 --> src/main.rs:3:24
  |
3 |     const len: usize = v.len();
  |     ---------          ^ non-constant value
  |     |
  |     help: consider using `let` instead of `const`: `let len`

For more information about this error, try `rustc --explain E0435`.
error: could not compile `playground` due to previous error

Why exactly is the above code giving me an error. In other languages I can assign the length of a vector or arraylist to a constant variable. Even though the length of a vec is mutable but the length of vec at the time of assignment must have been a constant value. So why am I getting this error?

And also how can I create an array of the same length as vector in rust?

Aditya
  • 175
  • 9
  • 1
    The whole point of a Rust array is that its length is knowable at compile time (i.e., it's a compile-time constant). A vector's length is only knowable at runtime. So, you cannot create an array whose length is the same as a vector. Other languages that claim to be able to create arrays dynamically probably aren't putting them on the stack and almost certainly can't treat them as `Copy`. – BallpointBen Feb 27 '22 at 01:51
  • 1
    @BallpointBen C# is one of the few modern languages I can think of that lets you do this (`stackalloc`) and since C# 7.2 doesn't require an unsafe context. – cdhowie Feb 27 '22 at 06:14

1 Answers1

1

Arrays in Rust are values (similar to arrays in C/C++) with contiguous storage for the elements where the array value lives, in comparison to languages like Java, C#, Python, etc., where contiguous storage for the elements is allocated elsewhere (the heap) and referenced from an array-typed variable.

Because of this, the size of an array in Rust must be known at compile time, because that determines how many bytes of memory are required for the array value.

If you want the flexibility of dynamically-sized arrays, then you need a separate allocation pointed at by a fixed-size struct that will manage the allocation for you. Thankfully, Rust comes with one of these types! It's called Vec!

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • Thanks for answering. I had another question that I had asked, why I was not able to assign const to the the length of the vector. For Example : const len: usize = v.len(); this should have been valid. – Aditya Feb 27 '22 at 12:29
  • @Aditya `const` declares a _compile-time_ constant, so the value must be fully known during compilation. However, `v.len()` does not have a known value at compile time. You declare run-time immutable values with `let` and run-time mutable values with `let mut`. If you are coming from C++, `const` in Rust is more like C++'s `constexpr` than C++'s `const`. – cdhowie Feb 27 '22 at 14:47