Attention epic rust programmers:
As we all know, one-liners always make for better code. Therefore I, in my journey of learning the power of rust, I have tried to make an insert-function for a struct representing a BinaryTree. For an extra challenge, I wanted it to mutate the Tree, not get a new one.
The struct is as follows:
pub type Node = i32;
type Branch = Option<Box<BinaryTree>>;
#[derive(Debug)]
pub struct BinaryTree {
node: Node,
left: Branch,
right: Branch
}
While my implementation for the insert-function should be:
impl BinaryTree {
pub fn insert(&mut self, node: Node) {
(if node <= self.node { &mut self.left } else { &mut self.right }) // get the correct branch
.as_deref_mut()
.map(|t| {t.insert(node); t}) // if the branch (Option) is Some, call insert onto the branch and return it
.get_or_insert(&mut Box::new(BinaryTree { node, left: None, right: None })); // if the branch is some (meaning it got mapped) then just return (do nothing), else insert a new Tree into the branch
}
}
... which compiles, but doesn't actually mutate the tree. So, the question is where have I gone wrong, since I explicitly state the branches as mutable.
Better one-linery versions are obviously sought after as well!
question from:
https://stackoverflow.com/questions/65916070/rust-conditional-mutating-within-one-liner 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…