Dyl1080
February 26, 2022, 6:48am
#1
I am a slight beginner in Rust and I am trying to set numbers 0-9 into word form. My code is erroring "and says expected type, found keyword static". I don't know what this means and I was hoping someone would help me understand and how to fix it.
pub fn run(){
fn run(n: usize) -> &static str {
let n:[i32; 10]=[0,1,2,3,4,5,6,7,8,9];
println!("{zero} {one} {two} {three} {four} {five} {six} {seven} {eight} {nine}", n[0,1,2,3,4,5,6,7,8,9,10]);
}}
moy2010
February 26, 2022, 7:54am
#2
There are a few issues in your code snippet:
println
returns the unit (()
) type, not &str
The bracket notation to access array elements by index only supports one element
String interpolation doesn't work like that in Rust, you are calling variables that are not defined anywhere in the code
2 Likes
2e71828
February 26, 2022, 10:07am
#3
Is something like this what you were aiming for?
fn main() {
for n in 0..10 {
println!("{n}: {}", run(n));
}
}
fn run(n: usize) -> &'static str {
const WORDS: [&'static str; 10] = [
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
];
&WORDS[n]
}
1 Like
Dyl1080
February 26, 2022, 10:42pm
#4
Thanks for the help and tips
Dyl1080
February 26, 2022, 10:43pm
#5
its saying "there is no argument named 'n'"
moy2010
February 26, 2022, 11:16pm
#6
Did you copy 2e71828's code from the playground? His code snippet is clearly working.
What version of the compiler are you using?
Dyl1080
February 27, 2022, 12:47am
#8
I just updated to 1.59 but it still doesn't work
Dyl1080
February 27, 2022, 2:46am
#9
I just found out it was a 0 width space unicode