> Comment above, mentioned borrowing while structs instead of borrowing memory. I believe this was once discussed under term 'partial borrows', but the "idiomatic" aproach is to 'just split your structs'.
You're mistaking "idomatic because it's the only way" with "idomatic because we say say so". There is currently no way in Rust to specify the granularity of a borrow, so we're stuck with splitting your structs to get around it.
Part of the problem is that it's a hard problem to design around. It can not be done automatically by the compiler, because it would result in changes in the implementation being changes in the type signature. For example, say we have this function:
pub fn foo(&self) -> i32 {
self.a + 5
}
The granularity is borrowing `a`. If we then change it to this:
pub fn foo(&self) -> i32 {
self.a + self.b
}
The granularity of the borrow has changed in a backwards incompatible way (it now includes `b`), but that change is not reflected in the signature. It's the same reason why the compiler refuses to infer signature lifetimes from function bodies now.
You could, of course, say that we can allow the programmer to specify it manually:
pub fn foo<'borrow>(&'borrow self) -> i32
where 'borrow: 'self.a + 'self.b
{
self.a + self.b
}
But this is now leaking implementation details if `a` or `b` are private fields.