I'm using MDBook to write documentation. I have some Rust code that works everywhere I've tried except in a {{#playpen}} or rust code block.
As I understand, {{#playpen}} and rust code blocks use Playground to run the code. I've tried running the code directly in Playground and it works fine. I've done everything I can to break it - nightly/beta/stable, release/debug, etc - it always works fine in Playground.
It works with cargo run on the command line. It works in CLion. I've used MDBook installed from crates.io and the latest installed from github
Can anyone see what I'm doing wrong?
Rob
Here is the code block:
```rust
trait Transmogrifier {
fn transmogrify(&mut self, name: &str) -> &str;
}
struct TransmogrifierProd {
}
fn main() {
struct Calvin<'a> {
transmogrifier: &'a mut Transmogrifier,
}
impl <'a> Calvin<'a> {
pub fn fire(&mut self, name: &str) -> &str {
self.transmogrifier.transmogrify(name)
}
}
struct TransmogrifierMock {
mock_return: String,
call_count: u16,
call_name: String
}
impl Transmogrifier for TransmogrifierMock {
fn transmogrify(&mut self, name: &str) -> &str {
self.call_name = name.to_string();
self.call_count += 1;
&self.mock_return
}
}
let mut mock = TransmogrifierMock {
mock_return: "Tiger".to_string(),
call_count: 0,
call_name: "".to_string(),
};
let mut calvin = Calvin { transmogrifier: &mut mock };
let fired = calvin.fire("Hobbs");
assert_eq!(fired, "Tiger");
assert_eq!(mock.call_name, "Hobbs");
assert_eq!(mock.call_count, 1);
}
```
Here is the error displayed only when I try to run it in MDBook:
Compiling playground v0.0.1 (/playground)
error[E0502]: cannot borrow `mock.call_name` as immutable because `mock` is also borrowed as mutable
--> src/main.rs:46:16
|
42 | let mut calvin = Calvin { transmogrifier: &mut mock };
| ---- mutable borrow occurs here
...
46 | assert_eq!(mock.call_name, "Hobbs");
| ^^^^^^^^^^^^^^ immutable borrow occurs here
...
49 | }
| - mutable borrow ends here
error[E0502]: cannot borrow `mock.call_count` as immutable because `mock` is also borrowed as mutable
--> src/main.rs:47:16
|
42 | let mut calvin = Calvin { transmogrifier: &mut mock };
| ---- mutable borrow occurs here
...
47 | assert_eq!(mock.call_count, 1);
| ^^^^^^^^^^^^^^^ immutable borrow occurs here
48 |
49 | }
| - mutable borrow ends here
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0502`.
error: Could not compile `playground`.
To learn more, run the command again with --verbose.