Format peut prendre pleins d'arguments #7

Open
opened 2022-01-13 14:53:13 +01:00 by darnuria · 1 comment
darnuria commented 2022-01-13 14:53:13 +01:00 (Migrated from code.up8.edu)

la macro format! qui utilise le trait Display peut prendre PLEINS d'arguments:

        let mut res: String = String::from("");
        res += &format!("ID => {}", self.id);

        res += &format!("\nType => {}", self.type_);

        res += &format!("\nNom => {}", self.name);

        res += &format!("\nNom d'affichage => {}", self.display_name);

Deviens:

        let res format!("ID => {}\nType => {}\nNom => {}\nNom d'affichage => {}",
            self.id,
            self.type,
            self.name,
            self.display_name
        );

(On peut faire des multilines strings aussi)

la macro format! qui utilise le trait `Display` peut prendre PLEINS d'arguments: ```rust let mut res: String = String::from(""); res += &format!("ID => {}", self.id); res += &format!("\nType => {}", self.type_); res += &format!("\nNom => {}", self.name); res += &format!("\nNom d'affichage => {}", self.display_name); ``` Deviens: ```rust let res format!("ID => {}\nType => {}\nNom => {}\nNom d'affichage => {}", self.id, self.type, self.name, self.display_name ); ``` (On peut faire des multilines strings aussi)
darnuria commented 2022-01-13 14:55:22 +01:00 (Migrated from code.up8.edu)

Bonus:

        if !self.description.is_empty() {
            res += &format!("\nDescription => {}", self.description);
        }

On peut faire plus simplement:

// Et on met ça dans le format general plus bas.
        if self.description.is_empty() {
             ""
        } else {
            format!("\nDescription => {}", self.description);
        }

Format est relativement optimisé aggressivement

Bonus: ```rust if !self.description.is_empty() { res += &format!("\nDescription => {}", self.description); } ``` On peut faire plus simplement: ```rust // Et on met ça dans le format general plus bas. if self.description.is_empty() { "" } else { format!("\nDescription => {}", self.description); } ``` Format est relativement optimisé aggressivement
This repo is archived. You cannot comment on issues.
No milestone
No project
No assignees
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: Paris8/MatterBot#7
No description provided.