Skip to content

Instantly share code, notes, and snippets.

@garrickp
Created November 3, 2014 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save garrickp/3b87852257f6cced56a6 to your computer and use it in GitHub Desktop.
Save garrickp/3b87852257f6cced56a6 to your computer and use it in GitHub Desktop.
Comparison not working
enum Ordering {
Less,
Equal,
Greater,
}
fn cmp(a: int, b: int) -> Ordering {
if a < b { Less }
else if a > b { Greater }
else { Equal }
}
fn main() {
let x = 5i;
let y = 6i;
let ordering = cmp(x, y);
if ordering == Less {
println!("less");
} else if ordering == Equal {
println!("equal");
} else {
println!("greater");
}
}
vagrant@vagrant-ubuntu-trusty-64:/vagrant/projects/hello_world$ cargo build && ./target/hello_world
Compiling hello_world v0.0.1 (file:///vagrant/projects/hello_world)
/vagrant/projects/hello_world/src/main.rs:19:5: 19:21 error: binary operation `==` cannot be applied to type `Ordering`
/vagrant/projects/hello_world/src/main.rs:19 if ordering == Less {
^~~~~~~~~~~~~~~~
/vagrant/projects/hello_world/src/main.rs:21:12: 21:29 error: binary operation `==` cannot be applied to type `Ordering
/vagrant/projects/hello_world/src/main.rs:21 } else if ordering == Equal {
^~~~~~~~~~~~~~~~~
error: aborting due to 2 previous errors
Could not compile `hello_world`.
To learn more, run the command again with --verbose.
vagrant@vagrant-ubuntu-trusty-64:/vagrant/projects/hello_world$ rustc --version
rustc 0.13.0-nightly (b87619e27 2014-11-02 23:27:10 +0000)
@geraldstanje
Copy link

enum Ordering {
    Less,
    Equal,
    Greater,
}

fn cmp(a: int, b: int) -> Ordering {
    if a < b { Less }
    else if a > b { Greater }
    else { Equal }
}

fn main() {
    let x = 5i;
    let y = 6i;

    let ordering = cmp(x, y);

    match ordering {
        Less => println!("less"),
        Equal => println!("equal"),
        Greater => println!("greater"),
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment