Guest User

Untitled

a guest
Jul 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. trait Foo {
  2. fn get<'a, 'b: 'a> (self: &'a Self) -> Option<&'b str>;
  3. fn insert (self: &mut Self, _: String);
  4. }
  5.  
  6. fn bar (foo: &mut Foo) // works !
  7. {
  8. if let Some(s_ref) = foo.get() {
  9. return println!("{}", s_ref)
  10. }
  11. foo.insert(42.to_string());
  12. bar(foo)
  13. }
  14.  
  15. fn bad (foo: &mut Foo) // error
  16. {
  17. if let Some(s_ref) = foo.get() {
  18. println!("{}", s_ref)
  19. } else {
  20. // idea: None: Option<&'mut_borrow_lt str> here explains the error
  21. foo.insert(42.to_string());
  22. bad(foo)
  23. }
  24. }
  25.  
  26. fn bad_unsugared (foo: &mut Foo) // error
  27. {
  28. match foo.get() {
  29. Some(s_ref) => println!("{}", s_ref),
  30. _ => {// idea: None: Option<&'mut_borrow_lt str> here explains the error
  31. foo.insert(42.to_string());
  32. bad_unsugared(foo)
  33. },
  34. }
  35. }
  36.  
  37. fn baz<'a, 'b: 'a>(foo: &'a mut Foo) -> &'b str // error??? why?
  38. {
  39. if let Some(s_ref) = foo.get() {
  40. return s_ref
  41. }
  42. // idea ??
  43. foo.insert(42.to_string());
  44. foo.get().unwrap()
  45. }
  46.  
  47. fn main () {}
Add Comment
Please, Sign In to add comment