# Why if/else expression in Rust doesn't end with a ;?

**URL:** <https://internals.rust-lang.org/t/why-if-else-expression-in-rust-doesnt-end-with-a/1098>\
**Category:** Uncategorized\
**Created:** [December 27, 2014, 7:27pm UTC](https://internals.rust-lang.org/t/why-if-else-expression-in-rust-doesnt-end-with-a/1098 "2014-12-27T19:27:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![maryjmcdermott57](https://avatars.discourse-cdn.com/v4/letter/m/e95f7d/32.png) [@maryjmcdermott57](https://internals.rust-lang.org/u/maryjmcdermott57)\
**Post date:** [December 27, 2014, 7:27pm UTC](https://internals.rust-lang.org/t/why-if-else-expression-in-rust-doesnt-end-with-a/1098/1 "2014-12-27T19:27:48Z")

</div>

In Rust, I have noticed that everything is an expression except 2 kinds of statements. Every expression that adds ; will become a statement. Rust’s grammar wants statements to follow other statements.

So why don’t we add ; at the end of an if / else “expression”? This is also an expression, so why don’t we do this:

```
if true {
    println!("true");
} else {
    println!("false");
};
```

---

<div class="post-metadata">

**Author:** ![kainino](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kainino/32/295_2.png) [@kainino](https://internals.rust-lang.org/u/kainino)\
**Post date:** [December 28, 2014, 1:57am UTC](https://internals.rust-lang.org/t/why-if-else-expression-in-rust-doesnt-end-with-a/1098/2 "2014-12-28T01:57:51Z")

</div>

My very limited understanding is as follows: While `if true { a(); } else { b(); }` is technically an expression, it’s almost like a statement since the value of the expression `{ a(); }` is unit: `()`. So while you _can_ put a semicolon at the end, it doesn’t really make sense to add that bit of syntactic noise.

I think that anywhere an expression has type `()` _and_ ends in `}`, the closing brace can ends the statement instead of requiring a semicolon, for convenience.

---

<div class="post-metadata">

**Author:** ![huon](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/huon/32/3698_2.png) [@huon](https://internals.rust-lang.org/u/huon)\
**Post date:** [December 28, 2014, 4:33am UTC](https://internals.rust-lang.org/t/why-if-else-expression-in-rust-doesnt-end-with-a/1098/3 "2014-12-28T04:33:53Z")

</div>

This has been extensively discussed on Reddit: [http://www.reddit.com/r/rust/comments/2qjvzr/why\_ifelse\_expression\_in\_rust\_doesnt\_end\_with\_a/](http://www.reddit.com/r/rust/comments/2qjvzr/why_ifelse_expression_in_rust_doesnt_end_with_a/)

(BTW, it’s generally polite to post a question to one place and then just link from any other places, rather than posting completely separately in 2 (3?) places. Or at least mention the cross-posting at each place. 😄)

---

<div class="post-metadata">

**Author:** ![maryjmcdermott57](https://avatars.discourse-cdn.com/v4/letter/m/e95f7d/32.png) [@maryjmcdermott57](https://internals.rust-lang.org/u/maryjmcdermott57)\
**Post date:** [December 28, 2014, 6:12am UTC](https://internals.rust-lang.org/t/why-if-else-expression-in-rust-doesnt-end-with-a/1098/4 "2014-12-28T06:12:02Z")

</div>

> [@huon](#):
>
> This has been extensively discussed on Reddit: [Reddit - Dive into anything](http://www.reddit.com/r/rust/comments/2qjvzr/why_ifelse_expression_in_rust_doesnt_end_with_a/)
> 
> (BTW, it's generally polite to post a question to one place and then just link from any other places, rather than posting completely separately in 2 (3?) places. Or at least mention the cross-posting at each place.)

I really apologize for my impoliteness. I don't know that. If you know how to close this topic please let me know. I will close it as soon as I know how to do that.

---

<div class="post-metadata">

**Author:** ![huon](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/huon/32/3698_2.png) [@huon](https://internals.rust-lang.org/u/huon)\
**Post date:** [December 28, 2014, 10:43am UTC](https://internals.rust-lang.org/t/why-if-else-expression-in-rust-doesnt-end-with-a/1098/5 "2014-12-28T10:43:51Z")

</div>

It’s not a problem: the downside of having threads in multiple locations is the discussion becomes fragmented and some people miss out, but that didn’t happen much here.

---

<div class="post-metadata">

**Author:** ![tutufan](https://avatars.discourse-cdn.com/v4/letter/t/f1d935/32.png) [@tutufan](https://internals.rust-lang.org/u/tutufan)\
**Post date:** [January 9, 2015, 7:36pm UTC](https://internals.rust-lang.org/t/why-if-else-expression-in-rust-doesnt-end-with-a/1098/6 "2015-01-09T19:36:54Z")

</div>

I read the reddit discussion, but still feel like I don’t entirely understand the rules. This is as opposed to C (for example), where I feel like I have a perfect understanding of how semicolons work, where they’re allowed, etc.

If would be nice if there was a concise rule or explanation for us newbies to chew on.

So far, my theory is that a semicolon is not a statement separator in Rust, nor does Rust have an “expression statement” like C. Beyond that, I’m just winging it, which feels uncomfortable.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:23am UTC](https://internals.rust-lang.org/t/why-if-else-expression-in-rust-doesnt-end-with-a/1098/7 "2019-03-25T08:23:26Z")

</div>

Closed on March 25, 2019, 8:23am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
