'Is there any way to transform a variable (let) to constant (const)?
Does Rust have a method to transform a variable declared as let to a constant?
I would like to initialize an array with the size defined by the variable (tam), as I couldn't, I initialized with the size 90000. Is there any way to do this initialization with the variable (tam)?
//the variable tam receives an i32 that represents the size of the input and I would
//like to initialize an array with this size -> //let mut cities: [[i32; 2]; tam] = [[0; 2]; tam];
let args: Vec<String> = env::args().collect();
let filename = &args[1];
let tam: &i32 = &args[2].parse::<i32>().unwrap();
//let mut cities: [[i32; 2]; tam] = [[0; 2]; tam];
let mut cities: [[i32; 2]; 90000] = [[0; 2]; 90000];
Solution 1:[1]
No it is not possible to convert a variable to a const
in Rust. Rust consts
are more like a C #define
than a variable, in that is in inlined, "copy-pasted", in all locations that uses it during compilation.
I would suggest making cities
a Vec
since that is something that you can dynamically size during runtime.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | evading |