“borrowed value does not live long enough” when using the builder pattern

Here’s a minimal reproduction: #[derive(Debug)] pub struct Canvas; impl Canvas { fn new() -> Self { Canvas } fn color(&self) -> &Canvas { self } } fn main() { let mut canvas = Canvas::new().color(); // 1 ^~~~~~~~~~~~~ // 2 ^~~~~ println!(“{:?}”, canvas); } Rust 2015 error[E0597]: borrowed value does not live long enough –> src/main.rs:15:22 … Read more

How to update-or-insert on a Vec?

There is a merged RFC “non-lexical lifetimes” which solves this in the long run. Using the non-lexical lifetimes in Rust 2018, available in Rust 1.31, your code works as-is: Playground use std::collections::HashMap; pub struct Pivot { pub min_key: u64, pub child: HashMap<u64, ()>, } fn update_or_append(pivots: &mut Vec<Pivot>, key: u64, value: ()) { match pivots.iter_mut().find(|ref … Read more