Please see the code in the playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=2cbfc0ce9c9daf317b13c47b1557a739
I am trying to implement the suggestions from the chapter:
" The implementation using the state pattern is easy to extend to add more functionality. To see the simplicity of maintaining code that uses the state pattern, try a few of these suggestions:
- Add a
rejectmethod that changes the post’s state from
- Require two calls to
approvebefore the state can be changed to
- Allow users to add text content only when a post is in the
Draftstate. Hint: have the state object responsible for what might change about the content but not responsible for modifying the
My implementation for the third feature involves having the state objects implement an add_text function that takes the text to be added as a parameter. Only the draft objects will return the text, the rest will substitute it with an empty string. Therefore the Post objects will then push whatever is returned.
I get the following compiler error on line 17:
self.content.push_str(self.state.as_ref().unwrap().add_text(text)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ move occurs because value has type `std::boxed::Box<dyn State>`, which does not implement the `Copy` trait
- Am I going about it the correct way? 2) How would I go about fixing this error? I understand it has something to do with ownership.
Please note I am a complete beginner.