“错误:预期的项目,发现'让'”

时间:2015-03-22 23:57:52

标签: rust

我有一个代码转储,我会为生锈代码添加示例,以防忘记。对于第41行,我不断获得error: expected item, found 'let'。 可能是我的代码没有正确的结构?我只是将我学到的代码片段粘贴到main.rs中。我认为枚举有某种特殊的格式或地点。

我尝试更改名称,认为这是名称惯例;但那并没有帮助。同样的错误。

这里是转储(实际上还不是那么大)

#[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);

1 个答案:

答案 0 :(得分:14)

您的基本问题是let只能在函数中使用。因此,将代码包装在main()中,并修复样式:

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);
 }