Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. struct Thing<'a> {
  2. thing_type: &'a ThingType,
  3. }
  4.  
  5. struct ThingType {
  6. id: u32,
  7. }
  8.  
  9. fn deserialize_thing<'a>(id: u32) -> Thing<'a> {
  10. // The function will own the value referred to by `thing_type`
  11. let thing_type = ThingType { id: id };
  12.  
  13. Thing { thing_type: &thing_type }
  14.  
  15. // thing_type goes out of scope and its value is dropped, but a `Thing` that
  16. // refers to it was returned. Hence we get a compiler error.
  17. }
  18.  
  19. fn main() {
  20. let thing = deserialize_thing(42);
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement