#Unnecessary borrow checker check?

3 messages · Page 1 of 1 (latest)

ivory steeple
#
struct SomeIterator<'a> {
    chars: Chars<'a>,
}

impl<'a> SomeIterator<'a> {
    fn is_something(&self, ch: Option<char>) -> bool {
        ch.is_some()
    }
}

impl<'a> Iterator for SomeIterator<'a> {
    type Item = ();

    fn next(&mut self) -> Option<Self::Item> {
        // This triggers the borrow checker
        self.is_something(self.chars.next()).then_some(())

        // But this doesn't...
        // let maybe_ch = self.chars.next();
        // self.is_something(maybe_ch).then_some(())
    }
}

I came across this borrow checker warning (I have constructed a contrived example to demonstrate), but discovered that simply by assigning to a variable before passing to a function satisfies the check.
Is this something that could simply be changed with the borrow checker, as it appears the solution for making it work appears trivial?

leaden whale
raven harbor
#

mhh I'm pretty sure this isn't related to NLL, but rather borrow phases. I doubt the code would be accepted under polonius.
the two versions are not strictly equivalent, as the first one borrows self for the call to is_something "before" producing the char, you can think of it as

Self::is_something(
    self, // borrowed here first
    Iterator::next(&mut self.chars) // borrowed mutably here afterwards
).[…]
```see <http://smallcultfollowing.com/babysteps/blog/2017/03/01/nested-method-calls-via-two-phase-borrowing/>