Skip to content

Instantly share code, notes, and snippets.

@smvv
Last active December 17, 2015 07:18
Show Gist options
  • Select an option

  • Save smvv/5571416 to your computer and use it in GitHub Desktop.

Select an option

Save smvv/5571416 to your computer and use it in GitHub Desktop.
struct Env {
out: ~str,
}
impl Env {
fn write(&self, msg: ~str) {
self.out += msg;
}
}
fn callback(env: @Env) {
env.write(~"hi");
}
fn main() {
let pattern = ~"a.*?";
let env = @Env {out: ~""};
callback(env);
io::println(fmt!("%s -> %s", pattern, env.out));
}
// test.rs:7:8: 7:16 error: assigning to immutable field
// test.rs:7 self.out += msg;
// ^~~~~~~~
// error: aborting due to previous error
struct Env {
out: ~str,
}
impl Env {
//fn write(&self, msg: ~str) {
fn write(&mut self, msg: ~str) {
self.out += msg;
}
}
//fn callback(env: @Env) {
fn callback(env: @mut Env) {
env.write(~"hi");
}
fn main() {
let pattern = ~"a.*?";
//let env = @Env {out: ~""};
let env = @mut Env {out: ~""};
callback(env);
//io::println(fmt!("%s -> %s", pattern, env.out));
io::println(fmt!("%s -> %s", pattern, copy env.out));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment