-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Open
Labels
A-NLLArea: Non-lexical lifetimes (NLL)Area: Non-lexical lifetimes (NLL)C-bugCategory: This is a bug.Category: This is a bug.NLL-poloniusIssues related for using Polonius in the borrow checkerIssues related for using Polonius in the borrow checkerT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
#![feature(nll)]
struct Node {
value: char,
children: Vec<Node>,
}
impl Node {
fn new(value: char) -> Self {
Self {
value,
children: Vec::new(),
}
}
fn add_child(&mut self, value: char) -> &mut Self {
self.children.push(Self::new(value));
self.children.last_mut().unwrap()
}
fn get_child(&mut self, value: char) -> Option<&mut Self> {
self.children.iter_mut().find(|n| n.value == value)
}
fn add_word(&mut self, word: String) {
let mut cursor = self;
for c in word.chars() {
// The extracted version of the function works
// cursor = cursor.get_or_add(c);
// But the inlined version of the function does not.
cursor = match cursor.get_child(c) {
Some(node) => node,
None => cursor.add_child(c),
};
}
}
fn get_or_add(&mut self, value: char) -> &mut Self {
match self.get_child(value) {
Some(node) => node,
None => self.add_child(value),
}
}
}
fn main() {}
error[E0499]: cannot borrow `*cursor` as mutable more than once at a time
--> src/main.rs:34:25
|
32 | cursor = match cursor.get_child(c) {
| ------ first mutable borrow occurs here
33 | Some(node) => node,
34 | None => cursor.add_child(c),
| ^^^^^^ second mutable borrow occurs here
schneiderfelipe, JB-Dmitry, fstephany, mo8it and MatrixDevschneiderfelipe
Metadata
Metadata
Assignees
Labels
A-NLLArea: Non-lexical lifetimes (NLL)Area: Non-lexical lifetimes (NLL)C-bugCategory: This is a bug.Category: This is a bug.NLL-poloniusIssues related for using Polonius in the borrow checkerIssues related for using Polonius in the borrow checkerT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.