you are viewing a single comment's thread
view the rest of the comments
[–] 6 points 11 months ago (4 children)
x = if y > 5 { "foo" } else { "bar" }

This is just superior to anything else

  • source
  • hideshow 8 child comments
  • [–] 2 points 11 months ago (2 children)

    I honestly can't see how this is more readable than

    x = (y > 5) ? "foo" : "bar"

    I get that it's a syntax that needs to be learned, but it's just so clean and concise!

  • source
  • parent
  • hideshow 4 child comments
  • [–] 3 points 11 months ago

    Because it can be done for multiple lines too. And you can do else-if too. Also, "if" and "else" is more recognizable than "?" and ":"

    x = if y > 5 {
        println!("Y was over 5");
        z + 5
    } else if y < 0 {
        handle_negative_y(y);
        z - y
    } else {
        println!("<WARN> unexpected value for y"}
        0
    }
    
  • source
  • parent
  • [–] 1 point 11 months ago (2 children)

    What I like about using if and else for that is that you're already using those keywords for branching in other parts of the code.

    Though my least favorite is probably Python's:

    x = "foo" if y > 5 else "bar"
    

    It just seems backwards to me

  • source
  • parent
  • hideshow 4 child comments
  • [–] 1 point 11 months ago* (1 child)

    While Python's version does feel a bit backwards, it's at least consistent with how list comprehensions are set up. They can also feel a bit "backwards" imo, especially when they include conditionals.

  • source
  • parent
  • hideshow 2 child comments