Guest User

Untitled

a guest
Apr 29th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. extern crate take_mut;
  2.  
  3. struct Dog {
  4. s: String,
  5. }
  6. struct Cat {
  7. s: String,
  8. }
  9. enum Content {
  10. SomethingA(Dog),
  11. SomethingB(Cat),
  12. }
  13.  
  14. struct A {
  15. content: Content,
  16. }
  17.  
  18. fn change(_dog: Dog) -> Cat {
  19. Cat {
  20. s: "lucy".to_string(),
  21. }
  22. }
  23.  
  24. fn does_not_work(it: &mut A) {
  25. take_mut::take(&mut it.content, |content| {
  26. match content {
  27. Content::SomethingA(dog) => Content::SomethingB(change(dog)),
  28. Content::SomethingB(cat) => Content::SomethingB(cat),
  29. }
  30. });
  31. }
  32.  
  33. fn main() {
  34. let mut it = A {
  35. content: Content::SomethingA(Dog {
  36. s: "wuff".to_string(),
  37. }),
  38. };
  39.  
  40. does_not_work(&mut it);
  41.  
  42. works();
  43. works2(&mut it);
  44. }
  45.  
  46. fn works() {
  47. let mut a = A {
  48. content: Content::SomethingA(Dog {
  49. s: "wuff".to_string(),
  50. }),
  51. };
  52.  
  53. match a.content {
  54. Content::SomethingA(dog) => {
  55. a.content = Content::SomethingB(change(dog));
  56. }
  57. Content::SomethingB(cat) => {
  58. a.content = Content::SomethingB(cat);
  59. }
  60. }
  61. }
  62.  
  63. fn works2(it: &mut A) {
  64. it.content = Content::SomethingA(Dog {
  65. s: "wuff".to_string(),
  66. });
  67. }
Add Comment
Please, Sign In to add comment