Skip to content

Instantly share code, notes, and snippets.

@ntalbott
Last active August 29, 2015 14:11
Show Gist options
  • Save ntalbott/797bbc88dc8ed2118456 to your computer and use it in GitHub Desktop.
Save ntalbott/797bbc88dc8ed2118456 to your computer and use it in GitHub Desktop.
Reduction showing challenge with a struct containing mutable and (intended) immutable fields.
ntalbott@lorica:~/play/rust-sandbox$ cargo build
Compiling rust-sandbox v0.0.1 (file:///Users/ntalbott/play/rust-sandbox)
/Users/ntalbott/play/rust-sandbox/src/main.rs:18:17: 18:39 error: cannot assign to `self.offset` because it is borrowed
/Users/ntalbott/play/rust-sandbox/src/main.rs:18 self.offset += c.len();
^~~~~~~~~~~~~~~~~~~~~~
/Users/ntalbott/play/rust-sandbox/src/main.rs:16:15: 16:19 note: borrow of `self.offset` occurs here
/Users/ntalbott/play/rust-sandbox/src/main.rs:16 match self.peek() {
^~~~
/Users/ntalbott/play/rust-sandbox/src/main.rs:29:41: 29:49 error: cannot borrow `s.offset` as immutable because `s` is also borrowed as mutable
/Users/ntalbott/play/rust-sandbox/src/main.rs:29 println!("char: {}, offset: {}", c, s.offset);
^~~~~~~~
note: in expansion of format_args!
<std macros>:2:23: 2:77 note: expansion site
<std macros>:1:1: 3:2 note: in expansion of println!
/Users/ntalbott/play/rust-sandbox/src/main.rs:29:5: 29:51 note: expansion site
/Users/ntalbott/play/rust-sandbox/src/main.rs:28:13: 28:14 note: previous borrow of `s` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `s` until the borrow ends
/Users/ntalbott/play/rust-sandbox/src/main.rs:28 let c = s.getch();
^
/Users/ntalbott/play/rust-sandbox/src/main.rs:30:2: 30:2 note: previous borrow ends here
/Users/ntalbott/play/rust-sandbox/src/main.rs:26 fn main() {
/Users/ntalbott/play/rust-sandbox/src/main.rs:27 let mut s = Scanner { string: "hello, world".to_string(), offset: 0 };
/Users/ntalbott/play/rust-sandbox/src/main.rs:28 let c = s.getch();
/Users/ntalbott/play/rust-sandbox/src/main.rs:29 println!("char: {}, offset: {}", c, s.offset);
/Users/ntalbott/play/rust-sandbox/src/main.rs:30 }
^
error: aborting due to 2 previous errors
Could not compile `rust-sandbox`.
use std::cell::Cell;
struct Scanner {
string: String,
offset: Cell<uint>
}
impl Scanner {
fn peek(&self) -> Option<&str> {
if self.offset.get() >= self.string.len() {
None
} else {
Some(self.string.slice_from(self.offset.get()).slice_chars(0, 1))
}
}
fn getch(&self) -> Option<&str> {
match self.peek() {
Some(c) => {
self.offset.set(self.offset.get() + c.len());
Some(c)
},
None => None
}
}
}
fn main() {
let s = Scanner { string: "hello, world".to_string(), offset: Cell::new(0) };
let c = s.getch();
println!("char: {}, offset: {}", c.unwrap(), s.offset.get());
}
struct Scanner {
string: String,
offset: uint
}
impl Scanner {
fn peek(&self) -> Option<&str> {
if self.offset >= self.string.len() {
None
} else {
Some(self.string.slice_from(self.offset).slice_chars(0, 1))
}
}
fn getch(&mut self) -> Option<&str> {
match self.peek() {
Some(c) => {
self.offset += c.len();
Some(c)
},
None => None
}
}
}
fn main() {
let mut s = Scanner { string: "hello, world".to_string(), offset: 0 };
let c = s.getch();
println!("char: {}, offset: {}", c, s.offset);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment