Writing poetry in Code

I am participating in the Mid-Autumn Festival Creative Submission contest, please see: Mid-Autumn Festival Creative Submission Contest for details

The Mid-Autumn Festival is coming ~~ I wish everyone happy moon Cake festival here. I saw the Nuggets article today, and there was a code poetry event, so I got a little something.

0x01 Select verse

Here are some poems to choose from at the Nuggets.

  • Looking up the bright moon, lower the head to think of home.
  • Cloudless mirror Kyushu, the most reunion night is Mid-Autumn Festival.
  • When does the moon appear? Ask the sky for wine.
  • We wish each other a long life so as to share the beauty of this graceful moonlight, even though miles apart.
  • Yesterday I wrote a poem to the white rabbit, laughing at his beautiful mistress.
  • Tonight month as hope, I do not know who autumn thought.
  • Raise a glass to invite the moon, to the shadow into three.
  • Recall the Mid-Autumn laurel plexus. The flowers are in the cup. The moon is in the cup.
  • Good season, wish every year, common autumn moon.
  • Every good night of the year, thousands of miles to see the light.
  • Chang e should regret stealing elixir, blue sea qingtian heart every night.
  • Know yutu is very round, has made frost wind cold September.

After reading these verses, they are really good poems. I’ll just pick one of them. As a result, the memory of the Mid-Autumn festival osmanthus plexus. The flowers are in the cup. The moon is in the cup. This line comes from song Dynasty poet Xin Qiji’s “Plum Blossom · Mid-Autumn Festival January.” The poem roughly translates as: “Recalling the past Mid-Autumn Festival, I was surrounded by fragrant cinnamon bushes. The flower reflected in the glass, the moon reflected in the glass.” It’s kind of nice.

0 x02 conception

Let’s start by looking at what this line has. There are flowers, the moon, wine glasses, cinnamon bushes, the time is Mid-Autumn Festival. Personally, the flower here refers to the osmanthus flower, that is, it can be understood that the osmanthus bush is composed of many flowers. The flowers are in the cup. The moon is in the cup. These two sentences feel easy to understand, translated into code is to add “flower” and “moon” to the wine glass.

0 x03 start Coding

Create structures and properties

First create the structure of the flower and moon

Struct Flower {pub(crate) id: i32,}Copy the code

Considering that there are flowers and moons in the cup, I defined the cup as a trait here and let Flower and Moon implement it.

/// 杯子
pub(crate) trait Cup {
    fn add_cup(&self);
}
Copy the code

Because there are osmanthus bushes and the moon in Mid-Autumn Festival, I also defined Mid-Autumn Festival as a trait. In fact, it can be defined as a struct.

/ / / pub Mid-Autumn festival (crate) trait MidAutumn {/ / / get the osmanthus flowers fn get_dan_gui_flowers (& self) - > Vec < Flower >; Fn get_moon(&self) -> Moon; }Copy the code

PS: Save the above code to mid_autumn.rs.

main.rs

Recall the Mid-Autumn Festival, the Mid-Autumn festival here refers to the past Mid-Autumn festival. Create a structure InThePastMidAutumn to implement the properties of MidAutumn.

Struct InThePastMidAutumn {name: &'static str, } from fn get_dan_gui_flowers(&self) -> Vec<Flower> {let mut Vec: Vec<Flower> = Vec::new(); for i in 0.. 5 { let flower = Flower { id: i }; vec.push(flower) } return vec; } fn get_moon(&self) -> Moon {return Moon {}; }}Copy the code

Continue to implement Cup features for Flower and Moon.

Impl Cup for Flower {fn add_cup(&self) {println! ("the flower id :{} in the cup!" , self.id); }} // impl Cup for Moon {fn add_cup(&self) {println! ("the moon in the cup!" ); }}Copy the code

0x04 Main function call

fn main() { let the_mid_autumn_day_in_the_past = InThePastMidAutumn { name: "Mid Autumn Day In the Past" }; println! ("{}", the_mid_autumn_day_in_the_past.name); for flower in the_mid_autumn_day_in_the_past.get_dan_gui_flowers().iter() { flower.add_cup(); } the_mid_autumn_day_in_the_past.get_moon().add_cup(); }Copy the code

Code running results:

Mid Autumn Day In the Past
the flower id :0 in the cup!
the flower id :1 in the cup!
the flower id :2 in the cup!
the flower id :3 in the cup!
the flower id :4 in the cup!
the moon in the cup!
Copy the code

0x05 Code improvements

In fact, when I was writing the code, I realized that there was only one moon. Consider implementing Moon as a singleton. The code for the singleton to implement Moon is as follows:

impl Moon {
    pub fn GetInstance() -> &'static Moon {
        static mut MOON_INSTANCE: Option<Arc<Moon>> = None;
​
        unsafe {
            MOON_INSTANCE.get_or_insert_with(|| {
                Arc::new(Moon {})
            });
            MOON_INSTANCE.as_ref().unwrap()
        }
    }
}
Copy the code

0x06 is written at the end

This activity is more interesting, I think you should have a better idea, I am here only to throw a brick to attract jade ~~. If you have any comments, you can leave me a message below ~