I did a stupid typo in a program, where I typed in twice in a for statement:
fn main() {
for i in in 1..2 {
println!("{}", i);
}
}
The compiler pointed the error on the closing brace, which, in the real code, with much more things happening in the loop (plus nested loops) was completely unhelpful.
error: expected one of `{` or an operator, found `}`
--> <anon>:5:1
|
5 | }
| ^
error: aborting due to previous error
Fixing the errors reported by the compiler reveals why this parses that far:
fn main() {
for i in in 1..2 {
println!("{}", i);
}
{ } }
error: placement-in expression syntax is experimental and subject to change. (see issue #27779)
--> <anon>:2:14
|
2 | for i in in 1..2 {
| ^
Current output points at the char immediately after the closing of the for scope, still needs some improvement:
error: expected one of `{` or an operator, found `}`
--> src/main.rs:5:1
|
4 | }
| - expected one of `{` or an operator here
5 | }
| ^ unexpected token
Closed by #48333:
error: expected expression, found keyword `in`
--> src/main.rs:2:14
|
2 | for i in in 1..2 {
| ^^ expected expression
That PR was reverted :(
use std::collections::HashMap;
fn main() {
let vec:Vec<i32> = vec![2,2,1];
single_number(vec);
}
fn single_number(nums: Vec<i32>) -> i32 {
let mut result = HashMap::new();
for v in vec! {
// 类型转换
let str_key = v.to_string();
//插入之前先判断,当前的key对应的value有值则加1,没值初始化为1
if result.get(&str_key) == None {
result.insert(str_key,1);
}else{
let str_key1 = v.to_string();
let _strValue = result.get(&str_key1);
let value = _strValue.unwrap();
result.insert(str_key,value + 1);
}
}
// 遍历result
let mut resultK = 0;
for (k,v) in &result {
println!("{},{}",k,v);
let value = *v as i32;
if value == 1
{
resultK = k.parse::<i32>().unwrap();;
}
}
resultK
}
Compiling playground v0.0.1 (/playground)
error: expected one of `.`, `?`, `{`, or an operator, found `let`
--> src/main.rs:30:5
|
25 | }
| - expected one of `.`, `?`, `{`, or an operator here
...
30 | let mut resultK = 0;
| ^^^ unexpected token
error: aborting due to previous error
error: Could not compile `playground`.
To learn more, run the command again with --verbose.
Most helpful comment
Fixing the errors reported by the compiler reveals why this parses that far: