Tuesday, April 19, 2022

Go vs Rust - Enum (switch vs match)

 1 Enum

In Golang, Enum implemented quite differently than most other programming languages. In Golang, we use a predeclared identifier, ​iota, and the enums are not strictly typed.

type Direction int
const (
    North Direction = iota
    South
    East
    West
)
    var myDirection Direction
    myDirection = West
    if (myDirection == West) {
      fmt.Println("myDirection is West:", myDirection)
    }

Rust’s enums are most similar to algebraic data types in functional languages, such as F#, OCaml, and Haskell.

enum WebEvent {
    // An `enum` may either be `unit-like`,
    PageLoad,
    // like tuple structs,
    Paste(String),
    // or c-like structures.
    Click { x: i64, y: i64 },
}

2 Switch Statement in Go
  • Both optstatement and optexpression in the expression switch are optional statements.
  • If both optstatementand optexpression are present, then a semi-colon(;) is required in between them.
  • If the switch does not contain any expression, then the compiler assume that the expression is true.
  • The optional statement, i.e, optstatement contains simple statements like variable declarations, increment or assignment statements, or function calls, etc.
  • If a variable present in the optional statement, then the scope of the variable is limited to that switch statement.
  • In switch statement, the case and default statement does not contain any break statement. But you are allowed to use break and fallthrough statement if your program required.
  • The default statement is optional in switch statement.
  • If a case can contain multiple values and these values are separated by comma(,).
  • If a case does not contain any expression, then the compiler assume that te expression is true.
Switch statement with both optional statement, i.e, day:=4 and expression, i.e, day
    switch day:=4; day{
       case 1:
       fmt.Println("Monday")
       default: 
       fmt.Println("Invalid")
   }

Expression switch statement
var value int = 2
switch {
       case value == 1:
       fmt.Println("Hello")
       default: 
       fmt.Println("Invalid")
 }

or

    var value string = "five"
      
    // Switch statement without default statement
    // Multiple values in case statement
   switch value {
       case "one":
       fmt.Println("C#")
       case "four", "five", "six":
       fmt.Println("Java")
   }  

Type switch statement

var value interface{}
switch q:= value.(type) {
       case bool:
       fmt.Println("value is of boolean type")
       default:
       fmt.Printf("value is of type: %T", q)       
}

3 Rust provides pattern matching via the match keyword, which can be used like a C switch. The first matching arm is evaluated and all possible values must be covered.

let number = 13;
match number {
        // Match a single value
        1 => println!("One!"),
        // Match several values
        2 | 3 | 5 | 7 | 11 => println!("This is a prime"),
        // Match an inclusive range
        13..=19 => println!("A teen"),
        // Handle the rest of cases
        _ => println!("Ain't special"),
}

or

fn inspect(event: WebEvent) {
    match event {
        WebEvent::PageLoad => println!("page loaded"),
        // Destructure `c` from inside the `enum`.
        WebEvent::KeyPress(c) => println!("pressed '{}'.", c),
        // Destructure `Click` into `x` and `y`.
        WebEvent::Click { x, y } => {
            println!("clicked at x={}, y={}.", x, y);
        },
    }
}







No comments:

Post a Comment