"error: expected item found by 'let'"

I have a code dump where I put rust code examples in case I forget something. I keep getting error: expected item, found 'let' for line 41+. Maybe my code is not structured properly? I just pasted the code snippets that I found out about into main.rs. I believe enumerations have some kind of special formatting or place.

I tried to change the names, considering that this is a naming convention; but it did not help. The same mistake.

Here's a dump (not too big yet)

 #[allow(dead_code)] fn main() { } /////////////////////////////////////////tutorial functoins i made fn if_statements() { //let (x, y) = (5, 10); let x = 5; let y = if x == 5 { 10 } else { 15 }; if y == 15 {println!("y = {}", y);} } ////////////////////////////////////////// tutoiral functions #[allow(dead_code)] fn add(a: i32, b: i32) -> i32 { a + b } #[allow(dead_code)] fn crash(exception: &str) -> ! { panic!("{}", exception); } //TUPLES// let y = (1, "hello"); let x: (i32, &str) = (1, "hello"); //STRUCTS// struct Point { x: i32, y: i32, } fn structs() { let origin = Point { x: 0, y: 0 }; // origin: Point println!("The origin is at ({}, {})", origin.x, origin.y); } //ENUMS// enum Character { Digit(i32), Other, } let ten = Character::Digit(10); let four = Character::Digit(4); 
+6
source share
1 answer

Your main problem is that let can only be used in a function. So, we wrap the code in main() , and also fix the style:

 fn if_statements() { let x = 5; let y = if x == 5 { 10 } else { 15 }; if y == 15 { println!("y = {}", y); } } #[allow(dead_code)] fn add(a: i32, b: i32) -> i32 { a + b } #[allow(dead_code)] fn crash(exception: &str) -> ! { panic!("{}", exception); } struct Point { x: i32, y: i32, } fn structs() { let origin = Point { x: 0, y: 0 }; println!("The origin is at ({}, {})", origin.x, origin.y); } enum Character { Digit(i32), Other, } fn main() { let y = (1, "hello"); let x: (i32, &str) = (1, "hello"); let ten = Character::Digit(10); let four = Character::Digit(4); } 
+9
source

Source: https://habr.com/ru/post/984115/


All Articles