我是 Rust 新手。我来自低水平、低功耗的微控制器 C 背景(不是 C++)和 PCB 设计。我正在尝试通过阅读《The Rust 编程语言》互动书来学习 Rust。我现在已经读完了第 3 章 - 流程控制。有一些练习 Rust 的练习任务,其中一项任务是:
Print the lyrics to the Christmas carol “The Twelve Days of Christmas,” taking advantage of the repetition in the song.
这是我的代码(尚未测试,需要先修复错误):
fn main() {
let numbering: [&str; 12] = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelveth"];
let gifts: [&str; 12] = ["A partridge in a pear tree", "Two turtle doves", "Three French hens", "Four calling birds", "Five golden rings", "Six geese a-laying", "Seven swans a-swimming", "Eight maids a-milking", "Nine ladies dancing", "Ten lords a-leaping", "Eleven pipers piping", "Twelve drummers drumming,"];
for i: u8 in 0..12 {
println!("On the {} day of Christmas,\n\r my true love sent to me", numbering[i]);
for j: u8 in 0..i.rev() {
println!("{}", gifts[j]);
}
}
}
在两个 for 循环上,我收到“语法错误:预期 IN_KW ”,在这些循环内部,我收到“在此范围内找不到值 'i/j' ”。
这可能是因为我缺乏知识,因为我目前处于本书的第一部分。也许这是一件微不足道的事情。请告诉我!
我正在使用 VSCode 进行编程。在 i: 和 j: 上使用 usize 会在 .rev() 上产生错误(无法在 usize 上执行 .rev())。
另外,我不能只使用
for i in 0..12
作为 Rust 分析器(?)中的自动完成功能,如果我不输入任何内容,则将 :usize 放在那里