Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate take_mut;
- struct Dog {
- s: String,
- }
- struct Cat {
- s: String,
- }
- enum Content {
- SomethingA(Dog),
- SomethingB(Cat),
- }
- struct A {
- content: Content,
- }
- fn change(_dog: Dog) -> Cat {
- Cat {
- s: "lucy".to_string(),
- }
- }
- fn does_not_work(it: &mut A) {
- take_mut::take(&mut it.content, |content| {
- match content {
- Content::SomethingA(dog) => Content::SomethingB(change(dog)),
- Content::SomethingB(cat) => Content::SomethingB(cat),
- }
- });
- }
- fn main() {
- let mut it = A {
- content: Content::SomethingA(Dog {
- s: "wuff".to_string(),
- }),
- };
- does_not_work(&mut it);
- works();
- works2(&mut it);
- }
- fn works() {
- let mut a = A {
- content: Content::SomethingA(Dog {
- s: "wuff".to_string(),
- }),
- };
- match a.content {
- Content::SomethingA(dog) => {
- a.content = Content::SomethingB(change(dog));
- }
- Content::SomethingB(cat) => {
- a.content = Content::SomethingB(cat);
- }
- }
- }
- fn works2(it: &mut A) {
- it.content = Content::SomethingA(Dog {
- s: "wuff".to_string(),
- });
- }
Add Comment
Please, Sign In to add comment