-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add exercise involving lifetime elision
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// lifetimes4.rs | ||
// | ||
// Sometimes, we have structs which hold on to data temporarily. A use-case of | ||
// this could be a routing component which accepts a connection and returns it to | ||
// another recipient. To avoid copying the data, we just accept a reference with | ||
// lifetime and return this reference later. | ||
// | ||
// In the example below, we create a `Router` instance in a limited scope. It | ||
// accepts a connection reference created in the enclosing scope and returns it. | ||
// In theory, this should be possible given that the connection reference outlives | ||
// the scope from which it is returned. However the borrow checker does not | ||
// seem to understand it. What can we do about that? | ||
// | ||
// Execute `rustlings hint lifetimes4` or use the `hint` watch subcommand for a | ||
// hint. | ||
|
||
// I AM NOT DONE | ||
|
||
struct Router<'a> { | ||
connection: Option<&'a u64>, | ||
} | ||
|
||
impl<'a> Router<'a> { | ||
fn new() -> Self { | ||
Self { connection: None } | ||
} | ||
|
||
fn accept_connection(&mut self, connection: &'a u64) { | ||
self.connection = Some(connection); | ||
} | ||
|
||
fn return_connection(&mut self) -> Option<&u64> { | ||
self.connection.take() | ||
} | ||
} | ||
|
||
fn main() { | ||
let connection = &123; | ||
|
||
let returned_connection = { | ||
// Create router within scope. | ||
let mut router = Router::new(); | ||
|
||
// Accept connection which lives longer than the router. | ||
router.accept_connection(connection); | ||
|
||
// Return connection which **should** live longer than the router. | ||
router.return_connection() | ||
}; | ||
|
||
if let Some(connection) = returned_connection { | ||
println!("The connection is {connection}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters