Advertisement
alexzander420

Untitled

Jul 9th, 2022
1,309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.66 KB | None | 0 0
  1. fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
  2.     if x.len() > y.len() {
  3.         x
  4.     } else {
  5.         y
  6.     }
  7. }
  8.  
  9. fn call_after() {
  10.     // lifetime 'a
  11.     let s1 = "asdas".to_string();
  12.  
  13.     // lifetime 'a (like return type of longest func)
  14.     let reference;
  15.     {
  16.         // lifetime 'b
  17.         let s2 = "hello wrlld".to_string();
  18.         // cant turn 'b into 'a here
  19.         reference = s2.as_str();
  20.     }
  21.     //                           'a     'b ('b doesnt exist anymore)
  22.     let result = longest(&s1, &reference);
  23.     //                               cant do that
  24.     println!("result: {}", result);
  25. }
  26.  
  27. fn main() {
  28.     call_after()
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement