Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. struct Foo;
  2. struct FooBuilder<'a> {
  3. foo: &'a Foo,
  4. }
  5.  
  6. impl Foo {
  7. fn build(&self) -> FooBuilder {
  8. FooBuilder { foo: &self }
  9. }
  10. }
  11.  
  12. impl<'a> FooBuilder<'a> {
  13. // correct: other lifetime for self
  14. fn enable<'b>(&'b mut self) -> &'b mut FooBuilder<'a> {
  15. self
  16. }
  17.  
  18. // wrong: can only call once
  19. fn disable(&'a mut self) -> &'a mut FooBuilder {
  20. self
  21. }
  22. }
  23.  
  24. fn main() {
  25. let foo = Foo {};
  26. let mut builder = foo.build();
  27. builder.enable();
  28. builder.disable();
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement