Why this code results in multiple mutable borrow?
struct DcdWriter<‘a>(&’a mut [u8]); impl<‘a> DcdWriter<‘a> { fn write_len<T: num_traits::ToPrimitive>( &’a mut self, value: T, len: usize, ) -> &’a mut DcdWriter<‘a> { dcd_encode(value.to_u32().unwrap(), &mut self.0[0..len]); self.0 = &mut self.0[len..]; self } fn write_var<T: num_traits::ToPrimitive>(&’a mut self, value: &T) -> &’a mut DcdWriter<‘a> { let len = std::mem::size_of_val(&value); self.write_len((*value).to_u32().unwrap(), len) } fn write_simple<‘b: ‘a>(&’b mut […]
Understanding Rust Lifetimes in Mixed Mutable and Immutable References
While reading an introductory book on Rust, I was stumped by a quiz. I chose the correct answer, but I found that my reasoning differed somewhat from the answer explanation.
Assigning RefCell method parameter to a local variable produces compile error
Consider the following simple example:
Lifetime error in implementation of (mutable) `Iterator` for custom linked list
I’m following the tutorial Learn Rust With Entirely Too Many Linked Lists to learn Rust. The aim is to learn the various aspects of Rust by implementing various levels of linked lists. I’m stuck at implementing the (mutable) Iterator
trait (here). These are my type definitions:
Second lifetime elision rule
The Rust book provides the following code to illustrate a valid function definition where explicit lifetimes are required: