(true, false) but 1 2 3 4?

What is going on here?

fn main(){
    println!("{:?}", (1<2,3>(4)))
}

(Playground)

1 is less than 2, 3 is not larger than 4.

8 Likes

If you run clippy (Rust's lintern), it will let you know about the unnecessary parentheses in your code, which would improve its readability.

2 Likes

Perhaps the missing piece here for the OP is that (a, b) creates a two-element tuple.

https://doc.rust-lang.org/reference/expressions/tuple-expr.html

1 Like

When confused by something, it helps to explain specifically what you find confusing, as well as what you expected to happen instead. Otherwise, people are left to guess what you mean by "What is going on here?"

Looking at this question, I genuinely don't know what aspect of this code you're asking about.

5 Likes

This code looks like a simplification of the bastion-of-the-turbofish. It's not useful for anything.

7 Likes

That's an utterly useless question to ask. What is going on here is some logical expressions are getting printed.

What did you expect?

1<2 is an expression that evaluates to the boolean value true. So we have:

(true,3>(4))

3>(4) is the same as 3>4 as the brackets are redundant. Which is a expression which evaluates to the boolean value false. So we have:

(true,false)

Which is a "tuple". That might be tricky if you have never used a language that had tuples. See: tuple - Rust.

Perhaps it's just me but I find such expressions easier to read if suitable spacing is used:

(1 < 2, 3 > (4))

Suggest using "rust fmt" or have your editor do the formatting.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.