I get the following errors for the "pseudo_rand_seq" function.
I need to pass a const to the function so that I can create an array with it. I am coming from a C background. How do I do this in Rust? Thanks
error: expected pattern, found keyword `const`
--> src\ts_36211.rs:119:20
|
119 | fn pseudo_rand_seq(const n_bits: u32, cinit: u32) -> u32 {
| ^^^^^ expected pattern
error[E0425]: cannot find value `n_bits` in this scope
--> src\ts_36211.rs:128:32
|
128 | let c: [u32; n_bits] = [0; n_bits];
| ^^^^^^ not found in this scope
Removing the const keyword throws the following error:
error[E0435]: attempt to use a non-constant value in a constant
--> src\ts_36211.rs:128:18
|
128 | let c: [u32; n_bits] = [0; n_bits];
| ^^^^^^ non-constant value
Oh, as I said earlier, this is a const-generic problem, which is not (yet) supported in rust. This makes working with sized arrays painful. I'd use a Vec like so:
fn pseudo_rand_seq(n_bits: u32, cinit: u32) -> u32 {
//...
let c = vec![0; n_bits];
//...
}
This uses the vec! macro which expands to a for loop which fills a Vec and returns it. Also, what are you trying to return at the end? You return -> u32 but (hypothetically) return -> [u32; n_bits]
const is a value known at compile time. Array sizes must be const so compiler can know its size at compile time, which allows placing them directly on stack memory without any heap allocation. Function parameters are value that selected by caller at runtime, thus it cannot be a const.