Created
February 18, 2013 21:37
-
-
Save eevee/4981007 to your computer and use it in GitHub Desktop.
lvalues can update themselves in Rust? (there is probably a nicer way to write this)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Flag { | |
X0 = 1, | |
X1 = 2, | |
X2 = 4, | |
X3 = 8, | |
X4 = 16, | |
} | |
type Flagset = uint; | |
trait FlagsetMethods { | |
fn add(&mut self, Flag); | |
fn has(Flag) -> bool; | |
} | |
impl Flagset: FlagsetMethods { | |
fn add(&mut self, f: Flag) { | |
*self |= (f as uint); | |
} | |
fn has(f: Flag) -> bool { | |
return self & (f as uint) != 0; | |
} | |
} | |
fn main() { | |
let mut flags: Flagset = 0; | |
(&mut flags).add(X0); | |
(&mut flags).add(X2); | |
io::println(fmt!("%?", flags.has(X0))); | |
io::println(fmt!("%?", flags.has(X1))); | |
io::println(fmt!("%?", flags.has(X2))); | |
io::println(fmt!("%?", flags.has(X3))); | |
io::println(fmt!("%?", flags.has(X4))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment