These are chat archives for rust-lang/rust

26th
Jan 2019
verilog15
@verilog15
Jan 26 12:12

Hello. I saw that Rust is an expression-oriented language. I was trying to somehow prove it by writing a program which prints some value (which is not ()).
What I had in mind:

fn main() {
    let x = if (5==5) { let y = 5;};
    println!("{:?}", x);
}

But it print () is it possible to achieve some other printed value which proves Rust being an expression-oriented language?

Tim Robinson
@1tgr
Jan 26 13:12
What do you mean by "expression-oriented"?
Possibly this program proves it; the block { let y = 5; } is an expression that evaluates to ()
And an if expression with no else also evaluates to ()
verilog15
@verilog15
Jan 26 13:19
expression-oriented language is a PL which supports the following Manichean - most expressions produce values (side-effects). It written in the Rust manual and in the Wikipedia that Rust supports the idea. I tried to write a program which produces other value then (). My program does prove it, but as I understand () is unit and the idea of idea can be misleading. I'm trying to somehow create a program which gets other value then ().
Tim Robinson
@1tgr
Jan 26 13:26

There's this phrase on the Wikipedia page:

Nearly every part of a function body is an expression, even control flow operators. For example, the ordinary if expression also takes the place of C's ternary conditional.

So you could have:
fn main() {
    let x = if (5==5) { "five equals five" } else { "five does not equal five" };
    println!("{:?}", x);
}
Or an example that's more specific to Rust: loop statements are expressions
If you use break; then the loop evaluates to (), but you can also break 5;