-
-
Save spoof/865ca5a4fb3ef287119b41e9bd4e5618 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[warn(unused_variables)] | |
| pub struct Letter { | |
| text: String, | |
| } | |
| impl Letter { | |
| pub fn new(text: String) -> Self { | |
| Letter { text } | |
| } | |
| } | |
| /// An empty pre-stamped envelope. | |
| pub struct EmptyEnvelope {} | |
| /// A closed envelope containing a letter. | |
| pub struct ClosedEnvelope { | |
| letter: Letter, | |
| } | |
| impl EmptyEnvelope { | |
| /// Put a letter in the envelope and seal it. | |
| pub fn wrap(self, letter: Letter) -> ClosedEnvelope { | |
| ClosedEnvelope { letter } | |
| } | |
| } | |
| pub struct PickupLorryHandle { | |
| done: bool, | |
| // references to lorry's resources | |
| envelope: Option<ClosedEnvelope>, | |
| } | |
| impl PickupLorryHandle { | |
| /// Give an envelope to the delivery driver. | |
| pub fn pickup(&mut self, envelope: ClosedEnvelope) { | |
| self.envelope = Some(envelope) | |
| } | |
| /// Tell the driver we don't have anything else for them. | |
| pub fn done(&mut self) { | |
| self.done = true; | |
| println!("sent"); | |
| } | |
| } | |
| impl Drop for PickupLorryHandle { | |
| fn drop(&mut self) { | |
| if !self.done { | |
| self.done(); | |
| } | |
| } | |
| } | |
| pub fn order_pickup() -> PickupLorryHandle { | |
| PickupLorryHandle { done: false, envelope: None } | |
| } | |
| pub fn buy_prestamped_envelope() -> EmptyEnvelope { | |
| EmptyEnvelope {} | |
| } | |
| // in a separate module | |
| fn main() { | |
| let rustfest_letter = Letter::new(String::from("Dear RustFest")); | |
| let envelope = buy_prestamped_envelope(); | |
| let closed_envelope = envelope.wrap(rustfest_letter); | |
| let eth_letter = Letter::new(String::from("Dear ETH")); | |
| let closed_envelope1 = envelope.wrap(eth_letter); | |
| let mut lorry = order_pickup(); | |
| lorry.pickup(closed_envelope); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment