Clone
Sometimes you want to make a copy of a value. The Clone trait accomplishes this.
#[derive(Default)]
struct Backends {
hostnames: Vec<String>,
weights: Vec<f64>,
}
impl Backends {
fn set_hostnames(&mut self, hostnames: &Vec<String>) {
self.hostnames = hostnames.clone();
self.weights = hostnames.iter().map(|_| 1.0).collect();
}
}
This slide should take about 2 minutes.
The idea of Clone is to make it easy to spot where heap allocations are occurring. Look for .clone() and a few others like Vec::new or Box::new.
It’s common to “clone your way out” of problems with the borrow checker, and return later to try to optimize those clones away.