|
| 1 | +// lifetimes4.rs |
| 2 | +// |
| 3 | +// Sometimes, we have structs which hold on to data temporarily. A use-case of |
| 4 | +// this could be a routing component which accepts a connection and returns it to |
| 5 | +// another recipient. To avoid copying the data, we just accept a reference with |
| 6 | +// lifetime and return this reference later. |
| 7 | +// |
| 8 | +// In the example below, we create a `Router` instance in a limited scope. It |
| 9 | +// accepts a connection reference created in the enclosing scope and returns it. |
| 10 | +// In theory, this should be possible given that the connection reference outlives |
| 11 | +// the scope from which it is returned. However the borrow checker does not |
| 12 | +// seem to understand it. What can we do about that? |
| 13 | +// |
| 14 | +// Execute `rustlings hint lifetimes4` or use the `hint` watch subcommand for a |
| 15 | +// hint. |
| 16 | + |
| 17 | +// I AM NOT DONE |
| 18 | + |
| 19 | +struct Router<'a> { |
| 20 | + connection: Option<&'a u64>, |
| 21 | +} |
| 22 | + |
| 23 | +impl<'a> Router<'a> { |
| 24 | + fn new() -> Self { |
| 25 | + Self { connection: None } |
| 26 | + } |
| 27 | + |
| 28 | + fn accept_connection(&mut self, connection: &'a u64) { |
| 29 | + self.connection = Some(connection); |
| 30 | + } |
| 31 | + |
| 32 | + fn return_connection(&mut self) -> Option<&u64> { |
| 33 | + self.connection.take() |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +fn main() { |
| 38 | + let connection = &123; |
| 39 | + |
| 40 | + let returned_connection = { |
| 41 | + // Create router within scope. |
| 42 | + let mut router = Router::new(); |
| 43 | + |
| 44 | + // Accept connection which lives longer than the router. |
| 45 | + router.accept_connection(connection); |
| 46 | + |
| 47 | + // Return connection which **should** live longer than the router. |
| 48 | + router.return_connection() |
| 49 | + }; |
| 50 | + |
| 51 | + if let Some(connection) = returned_connection { |
| 52 | + println!("The connection is {connection}"); |
| 53 | + } |
| 54 | +} |
0 commit comments