Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile error in enums code #93

Open
MarcSeebold opened this issue Apr 27, 2020 · 1 comment
Open

Compile error in enums code #93

MarcSeebold opened this issue Apr 27, 2020 · 1 comment

Comments

@MarcSeebold
Copy link

MarcSeebold commented Apr 27, 2020

In https://github.com/nrc/r4cppp/blob/master/data-types.md you write:

enum Expr {
    Add(i32, i32),
    Or(bool, bool),
    Lit(i32)
}

fn foo() {
    let x = Or(true, false);   // x has type Expr
}

This code throws the following errors:

error[E0425]: cannot find function, tuple struct or tuple variant 'Or' in this scope
   --> enums.rs:10:13
    |
10  |     let x = Or(true, false); // x has type Expr
    |             ^^
    |
help: a tuple variant with a similar name exists
    |
10  |     let x = Ok(true, false); // x has type Expr
    |             ^^
help: possible candidate is found in another module, you can import it into scope
    |
1   | use Expr::Or;
    |

error: aborting due to previous error

For more information about this error, try 'rustc --explain E0425'..

rustc 1.42.0 (b8cedc004 2020-03-09)

Fix: Add namespace
let x = Expr::Or(true, false)

Same on the snippet below:

fn bar(e: Expr) {
    match e {
        Add(x, y) => println!("An `Add` variant: {} + {}", x, y),
        Or(..) => println!("An `Or` variant"),
        _ => println!("Something else (in this case, a `Lit`)"),
    }
}

Same fix. Alternative:

use Expr::Add;
use Expr::Or;
@nrc
Copy link
Owner

nrc commented Apr 27, 2020

Using the explicit prefix is the idiomatic fix - this example is so old that it uses the old enum rules!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants