Zum Inhalt springen

Kontrollfluss

Welches Kontrollfluss-Konstrukt wäre am idiomatischsten in der folgenden Funktion zu verwenden?

enum Location {
Point(i32),
Range(i32, i32)
}
fn print_range_max(loc: &Location) {
// print the second field of Range, if loc is a Range
}

Wenn die Funktion nur unter einer Bedingung eine Wirkung hat, ist ein if let am idiomatischsten.


Welches Kontrollfluss-Konstrukt wäre am idiomatischsten in der folgenden Funktion zu verwenden?

enum Location {
Point(i32),
Range(i32, i32)
}
fn get_start(loc: &Location) -> i32 {
// return the first field of Range or the only field of Point
}

Wenn die Funktion für jede Bedingung einen Wert zurückgeben muss, ist ein match am besten geeignet.