# Ternary Operator

**URL:** <https://internals.rust-lang.org/t/ternary-operator/23908>\
**Category:** Uncategorized\
**Created:** [January 9, 2026, 9:56am UTC](https://internals.rust-lang.org/t/ternary-operator/23908 "2026-01-09T09:56:34Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Svelte-X-BORODINO](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/svelte-x-borodino/32/13956_2.png) [@Svelte-X-BORODINO](https://internals.rust-lang.org/u/Svelte-X-BORODINO)\
**Post date:** [January 9, 2026, 9:56am UTC](https://internals.rust-lang.org/t/ternary-operator/23908/1 "2026-01-09T09:56:34Z")

</div>

Please add ternary operator support

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [January 9, 2026, 10:11am UTC](https://internals.rust-lang.org/t/ternary-operator/23908/2 "2026-01-09T10:11:14Z")

</div>

We have a replacement for that already. `if a { b } else { c }` is equivalent to `a ? b : c` in C. Yes, you can use `if` in expression contexts.

---

<div class="post-metadata">

**Author:** ![Svelte-X-BORODINO](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/svelte-x-borodino/32/13956_2.png) [@Svelte-X-BORODINO](https://internals.rust-lang.org/u/Svelte-X-BORODINO)\
**Post date:** [January 9, 2026, 10:28am UTC](https://internals.rust-lang.org/t/ternary-operator/23908/3 "2026-01-09T10:28:32Z")

</div>

i know, but “a ? b : c” is shorter

Пт, 9 янв. 2026 г. в 13:21, bjorn3 \<[notifications@rustlang.discoursemail.com](mailto:notifications@rustlang.discoursemail.com)\>:

---

<div class="post-metadata">

**Author:** ![IndianBoy42](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/indianboy42/32/7138_2.png) [@IndianBoy42](https://internals.rust-lang.org/u/IndianBoy42)\
**Post date:** [January 9, 2026, 1:08pm UTC](https://internals.rust-lang.org/t/ternary-operator/23908/4 "2026-01-09T13:08:07Z")

</div>

> **[Frequently Requested Changes - The Rust Language Design Team](https://lang-team.rust-lang.org/frequently-requested-changes.html)**

I'm surprised ternary isn't mentioned specifically here but nevertheless the spirit is there

---

<div class="post-metadata">

**Author:** ![HjVT](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hjvt/32/10365_2.png) [@HjVT](https://internals.rust-lang.org/u/HjVT)\
**Post date:** [January 9, 2026, 1:16pm UTC](https://internals.rust-lang.org/t/ternary-operator/23908/5 "2026-01-09T13:16:21Z")

</div>

I do wish there was a slightly more succinct way to write `if a { b } else { c }`, even just teaching `rustfmt` to be more conservative about spreading ifs in expression position across multiple lines would help.

---

<div class="post-metadata">

**Author:** ![SkiFire13](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skifire13/32/7579_2.png) [@SkiFire13](https://internals.rust-lang.org/u/SkiFire13)\
**Post date:** [January 9, 2026, 2:38pm UTC](https://internals.rust-lang.org/t/ternary-operator/23908/6 "2026-01-09T14:38:27Z")

</div>

> [@HjVT](#):
>
> even just teaching `rustfmt` to be more conservative about spreading ifs in expression position across multiple lines would help.

There's already a [setting](https://rust-lang.github.io/rustfmt/?version=v1.8.0&search=#single_line_if_else_max_width) for that, or do you mean making the default more conservative?

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [January 9, 2026, 6:23pm UTC](https://internals.rust-lang.org/t/ternary-operator/23908/7 "2026-01-09T18:23:42Z")

</div>

> [@IndianBoy42](#):
>
> I'm surprised ternary isn't mentioned specifically here

PRs welcome 🙂

I agree that it both comes up often and that we're extremely unlike to add `?:`, so might as well put it there.

---

<div class="post-metadata">

**Author:** ![tczajka](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tczajka/32/8923_2.png) [@tczajka](https://internals.rust-lang.org/u/tczajka)\
**Post date:** [January 10, 2026, 3:53am UTC](https://internals.rust-lang.org/t/ternary-operator/23908/8 "2026-01-10T03:53:31Z")

</div>

> [@HjVT](#):
>
> I do wish there was a slightly more succinct way to write `if a { b } else { c }`

If instead of the `if` keyword you had a suffix keyword (`then`), the braces would be unnecessary for disambiguation, except for the dangling `else` problem for nested `if`s:

```rust
x < 0 then -x else x

```

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jrose/32/9591_2.png) [@jrose](https://internals.rust-lang.org/u/jrose)\
**Post date:** [January 10, 2026, 5:49am UTC](https://internals.rust-lang.org/t/ternary-operator/23908/9 "2026-01-10T05:49:37Z")

</div>

`si!(a, b, c)`. 9 characters vs 18, counting the spaces but not the expressions…but still 6 tokens, so is it really shorter?

(“si” being Spanish for “if”)

---

<div class="post-metadata">

**Author:** ![197g](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/197g/32/7276_2.png) [@197g](https://internals.rust-lang.org/u/197g)\
**Post date:** [January 10, 2026, 7:53pm UTC](https://internals.rust-lang.org/t/ternary-operator/23908/10 "2026-01-10T19:53:00Z")

</div>

Building on `select_unpredictable`, maybe this would be okay naming-wise:

```rust
(x < 0).select(-x, x)

```

I don't think I'd ever want to have such a method where either case or both are `impl Fn` to be computed demand, that should stay a block.

---

<div class="post-metadata">

**Author:** ![mathstuf](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mathstuf](https://internals.rust-lang.org/u/mathstuf)\
**Post date:** [January 11, 2026, 2:00am UTC](https://internals.rust-lang.org/t/ternary-operator/23908/11 "2026-01-11T02:00:27Z")

</div>

> [@197g](#):
>
> I don't think I'd ever want to have such a method where either case or both are `impl Fn` to be computed demand, that should stay a block.

And if you want a lazy-but-function-divergent path (e.g., `return format!("{expensive_format}"))`?

I'd hate transforming between the two if I needed that and style pressure to prefer one of the other may make code less consistent or more contorted to conform. I see the appeal for an ACP, but I don't know if it's something to recommend in any default-warn `clippy` lint.

---

<div class="post-metadata">

**Author:** ![flirora](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/flirora/32/12267_2.png) [@flirora](https://internals.rust-lang.org/u/flirora)\
**Post date:** [January 19, 2026, 9:04pm UTC](https://internals.rust-lang.org/t/ternary-operator/23908/12 "2026-01-19T21:04:06Z")

</div>

Note that Rust used to have a ternary operator a long time ago but [it was removed](https://github.com/rust-lang/rust/pull/1705) because it was redundant with `if`-expressions, so to say the least, it’s unlikely to be added back.
