Skip to content

Instantly share code, notes, and snippets.

@SebastianEdwards
Last active December 15, 2015 09:58
Show Gist options
  • Save SebastianEdwards/5241927 to your computer and use it in GitHub Desktop.
Save SebastianEdwards/5241927 to your computer and use it in GitHub Desktop.
Any help appreciated!
string_builder.rs:11:35: 11:40 error: assigning to dereference of mutable & pointer prohibited due to outstanding loan
string_builder.rs:11 PartialContent(ref vec) => { *self = FullContent(str::from_chars(*vec)); },
^~~~~
string_builder.rs:9:10: 9:15 note: loan of dereference of mutable & pointer granted here
string_builder.rs:9 match *self {
^~~~~
string_builder.rs:23:8: 23:13 error: assigning to dereference of mutable & pointer prohibited due to outstanding loan
string_builder.rs:23 *self = PartialContent(*vec);
^~~~~
string_builder.rs:17:10: 17:15 note: loan of dereference of mutable & pointer granted here
string_builder.rs:17 match *self {
^~~~~
string_builder.rs:23:31: 23:35 error: moving out of dereference of mutable & pointer
string_builder.rs:23 *self = PartialContent(*vec);
^~~~
error: aborting due to 3 previous errors
enum StringBuilder {
NoContent,
PartialContent(~[char]),
FullContent(~str),
}
impl StringBuilder {
fn finalize(&mut self) {
match *self {
NoContent => { },
PartialContent(ref vec) => { *self = FullContent(str::from_chars(*vec)); },
FullContent(_) => { },
}
}
fn push(&mut self, c: char) {
match *self {
NoContent => {
*self = PartialContent(~[c]);
},
PartialContent(ref mut vec) => {
vec.push(c);
*self = PartialContent(*vec);
},
FullContent(_) => { },
}
}
fn to_str(&self) -> ~str {
match *self {
NoContent => { ~"" },
PartialContent(ref vec) => { str::from_chars(*vec) },
FullContent(ref string) => { copy *string },
}
}
}
fn main() {
let mut string_builder = NoContent;
string_builder.push('h');
string_builder.push('e');
string_builder.push('l');
string_builder.push('l');
string_builder.push('o');
string_builder.finalize();
io::println(string_builder.to_str());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment