Capture mutable and immutable reference at the same time with different lifetimes

I'm still learning Rust. Now I tried to create some sort of restricted LinkedList using lifetimes.
To accomplish what I wanted to do I had to split up my insert method:

pub fn insert1<'lifetime_node>(&self, node: &'lifetime_node mut ListNode<'lifetime_list, T>) -> () 
{
	node.next = self.next;
}
	
pub fn insert2(&mut self, node: &'lifetime_list ListNode<'lifetime_list, T>) -> () 
{
	self.next = Option::<&Self>::Some(node);
}

Is there a way to put both into one function while keeping the mutable reference only for as long as the function call but the immutable reference for longer?

No there isn’t, and the impossibility of expressing such borrowing patterns indeed is a known limitation of Rust function signatures.

The best you can do is let it stay two separate function calls, and possibly bundle them up with a macro.

1 Like

eye-roll

Okay thanks for your fast reply. I'm gonna add that to my list of stuff I'd attempt to optimize in Rust language.