I just read through the first three chapters of the Rust Programming Language, and am currently trying to apply my limited knowledge building a CLI weather app with the help of a YouTube tutorial.
I mostly have little idea what I'm doing, but just wanted to make something to inspire learning momentum.
May someone share some explanations on what I did wrong?
I tried compiling Rust code and failed with a syntax error.
fn main() {
for (x: String, ) in [("hello".into(),)] {}
}
error: expected one of `)`, `,`, `@`, or `|`, found `:`
--> src/main.rs:2:11
|
2 | for (x: String, ) in [("hello".into(),)] {}
| ^ expected one of `)`, `,`, `@`, or `|`
error: could not compile `forecast` (bin "forecast") due to 1 previous error
As Ivan C commented, you can't have types inside patterns, which is why the left hand side of a
foris. Nor doesforsupport type annotations (as you can see -- or not see -- in the link above), so if you update the code to what you'd find in aletfor instance:you will get
There are two solutions to this:
ensure the value is typed, instead of using
"hello".into()you can useString::from("hello")or"hello".to_string(), that will ensure the value is correctly typed and solve the issueuse iterators, as those take functions, which can be typed:
I would personally recommend the former.
Or really you could just iterate on the
&strthemselves, it works fine, and only convert to aStringif ans where needed.