I am new to rust and am trying to work through reference lifetimes. I am unable to understand why the program compiles if `self.rate.insert(&vertex, inner_matrix)`

is uncommented.

```
pub struct Vertex {
pub exchange: String,
pub currency: String,
pub last_updated: u64
}
impl Vertex {
pub fn new(exchange: String, currency: String, last_updated: u64) -> Vertex {
Vertex {
exchange, currency, last_updated
}
}
}
impl Hash for Vertex {
fn hash<H: Hasher>(&self, state: &mut H) {
self.exchange.hash(state);
self.currency.hash(state);
}
}
impl PartialEq for Vertex {
fn eq(&self, other: &Vertex) -> bool {
self.exchange == other.exchange && self.currency == other.currency
}
}
#[derive(Debug)]
pub struct Graph<'a> {
vertices: HashSet<&'a Vertex>,
rate: HashMap<&'a Vertex, HashMap<&'a Vertex, f64>>, // stores the edge weights between each pair of vertex
next: HashMap<&'a Vertex, HashMap<&'a Vertex, &'a Vertex>>, // stores vertices to reconstruct the path for best rate from vertex i to j
}
impl<'a> Graph<'a> {
pub fn new() -> Graph<'a> {
Graph {
vertices: HashSet::new(),
rate: HashMap::new(),
next: HashMap::new()
}
}
pub fn add_vertex(&mut self, vertex: Vertex) {
match self.vertices.get(&vertex) {
Some(inner_matrix) => {
// Do stuff
},
None => {
let mut inner_matrix: HashMap<&Vertex, f64> = HashMap::new();
inner_matrix.insert(&vertex, 0.0);
// Error if uncomment any line below
self.rate.insert(&vertex, inner_matrix);
// self.vertices.insert(&vertex);
}
}
}
}
```

**Compiler error**

impl<'a> Graph<'a> {

| – lifetime `'a`

defined here

…

142 | inner_matrix.insert(&vertex, 0.0);

| ^^^^^^^ borrowed value does not live long enough

…

145 | self.rate.insert(&vertex, inner_matrix);

| --------------------------------------- argument requires that `vertex`

is borrowed for `'a`

…

149 | }

| - `vertex`

dropped here while still borrowed