# Addition to rust's int

**URL:** <https://internals.rust-lang.org/t/addition-to-rusts-int/18879>\
**Category:** language design\
**Created:** [May 25, 2023, 4:04pm UTC](https://internals.rust-lang.org/t/addition-to-rusts-int/18879 "2023-05-25T16:04:16Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaspberryBerries](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/raspberryberries/32/10891_2.png) [@RaspberryBerries](https://internals.rust-lang.org/u/RaspberryBerries)\
**Post date:** [May 25, 2023, 4:04pm UTC](https://internals.rust-lang.org/t/addition-to-rusts-int/18879/1 "2023-05-25T16:04:17Z")

</div>

I would like to add the possibility of adding, multiplying and subtracting bool from int that is, have implement with bool from int.

---

<div class="post-metadata">

**Author:** ![toc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/toc/32/6692_2.png) [@toc](https://internals.rust-lang.org/u/toc)\
**Post date:** [May 25, 2023, 4:12pm UTC](https://internals.rust-lang.org/t/addition-to-rusts-int/18879/2 "2023-05-25T16:12:47Z")

</div>

I might go further and do this for all strictly widening (same-signed?) numerical operations, but for better or worse in rust this is currently spelled

```rust
my_int + my_bool.into()

```

Possibly this is just a small ergonomics hit that is just waiting on some way to set inference defaults, possibly some believe this is _the way things should be_.

---

<div class="post-metadata">

**Author:** ![mjbshaw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mjbshaw/32/5103_2.png) [@mjbshaw](https://internals.rust-lang.org/u/mjbshaw)\
**Post date:** [May 25, 2023, 5:44pm UTC](https://internals.rust-lang.org/t/addition-to-rusts-int/18879/4 "2023-05-25T17:44:08Z")

</div>

While I generally wouldn't mind automatic integer widening, I personally like that Rust doesn't auto-convert `bool` to int. Strict typing has saved me from some (dumb) mistakes.

---

<div class="post-metadata">

**Author:** ![rkuhn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rkuhn/32/6659_2.png) [@rkuhn](https://internals.rust-lang.org/u/rkuhn)\
**Post date:** [May 26, 2023, 9:11am UTC](https://internals.rust-lang.org/t/addition-to-rusts-int/18879/5 "2023-05-26T09:11:05Z")

</div>

This idea presumes that `true` and `false` have integer meanings. The fact that C represents them as 1 an 0 may provide context but does not automatically provide this meaning. For example, why should `5 - true` not be zero? (consider that in C five is also true) True and false do not inherently have integer value, they are from a different category.

This is especially obvious when considering `true + true`: should it be `false` (wrapping around in Z\_2) or `true` (logical addition) or 2 (since `true == 1`)? (the last condition must hold under your proposal because `5 + true == 6`, solving for `true` yields 1)

---

<div class="post-metadata">

**Author:** ![quaternic](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quaternic/32/10440_2.png) [@quaternic](https://internals.rust-lang.org/u/quaternic)\
**Post date:** [May 26, 2023, 1:28pm UTC](https://internals.rust-lang.org/t/addition-to-rusts-int/18879/6 "2023-05-26T13:28:09Z")

</div>

> [@rkuhn](#):
>
> This idea presumes that `true` and `false` have integer meanings. The fact that C represents them as 1 an 0 may provide context but does not automatically provide this meaning. For example, why should `5 - true` not be zero? (consider that in C five is also true) True and false do not inherently have integer value, they are from a different category.

Rust does agree with C in that `false` is `0` and `true` is `1`, it just requires the type cast to be explicit: `true as u8 == 1`. What Rust rightly avoided is the other direction of giving integer values an inherent interpretation as truth values, instead requiring spelling it out like `x != 0`.

---

<div class="post-metadata">

**Author:** ![rkuhn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rkuhn/32/6659_2.png) [@rkuhn](https://internals.rust-lang.org/u/rkuhn)\
**Post date:** [May 26, 2023, 2:15pm UTC](https://internals.rust-lang.org/t/addition-to-rusts-int/18879/7 "2023-05-26T14:15:37Z")

</div>

Right. My post was intended as an argument to stop this unholy conflation (😉) where it currently is and let it proceed no further. In my opinion, the `.into()` and `as u8` are mistakes already.

---

<div class="post-metadata">

**Author:** ![user16251](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/user16251/32/10186_2.png) [@user16251](https://internals.rust-lang.org/u/user16251)\
**Post date:** [May 26, 2023, 3:31pm UTC](https://internals.rust-lang.org/t/addition-to-rusts-int/18879/8 "2023-05-26T15:31:19Z")

</div>

Iverson bracket notation (i.e., `[true] = 1` and `[false] = 0`) is incredibly useful. In a language like R it is idiomatic to use bracket notation; you can write `mean(x > 0)` to estimate the probability that `X > 0` (if `x` is a vector of samples of the random variable `X`). I believe that it should be explicit but without `.into()` how would you propose writing it?

---

<div class="post-metadata">

**Author:** ![rkuhn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rkuhn/32/6659_2.png) [@rkuhn](https://internals.rust-lang.org/u/rkuhn)\
**Post date:** [May 26, 2023, 4:40pm UTC](https://internals.rust-lang.org/t/addition-to-rusts-int/18879/9 "2023-05-26T16:40:10Z")

</div>

Not sure what exactly you're asking (I don't know R and R syntax is not relevant to Rust), but if it is about getting from `bool` to `u8` I'd prefer a specific method like `bool::to_u8(self)`. The other direction is easily done using integer comparison operators.

---

<div class="post-metadata">

**Author:** ![user16251](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/user16251/32/10186_2.png) [@user16251](https://internals.rust-lang.org/u/user16251)\
**Post date:** [May 26, 2023, 5:00pm UTC](https://internals.rust-lang.org/t/addition-to-rusts-int/18879/10 "2023-05-26T17:00:45Z")

</div>

You should learn some R! It is a beautiful language and it is very useful as a desk calculator. (Unfortunately Python seems to be eating the world.) Just because it's not Rust doesn't mean it couldn't be relevant to a discussion of Rust.

I was just trying to emphasize that having succinct notation to convert `bool` to an integer is extremely important. I don't think it should be implicit but if anything the current syntax is too verbose (I am not proposing an alternative though).

---

<div class="post-metadata">

**Author:** ![tvallotton](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tvallotton/32/8153_2.png) [@tvallotton](https://internals.rust-lang.org/u/tvallotton)\
**Post date:** [May 29, 2023, 1:57pm UTC](https://internals.rust-lang.org/t/addition-to-rusts-int/18879/11 "2023-05-29T13:57:21Z")

</div>

it is sometimes useful to write branchless code. For the most part the compiler is able to optimize simple branches, but it doesn't always work.

---

<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:** [May 29, 2023, 3:23pm UTC](https://internals.rust-lang.org/t/addition-to-rusts-int/18879/12 "2023-05-29T15:23:30Z")

</div>

> [@user16251](#):
>
> Iverson bracket notation (i.e., `[true] = 1` and `[false] = 0`) is incredibly useful.

`[true]` and `[false]` are already arrays containing a single `bool` in Rust, so this doesn't seem applicable here. Also, at first sight I would not say that the operation being done is a conversion to an integer, as opposed to `true as u8` and `false as u8`.

> [@user16251](#):
>
> In a language like R it is idiomatic to use bracket notation; you can write `mean(x > 0)` to estimate the probability that `X > 0` (if `x` is a vector of samples of the random variable `X`).

What is the bracket notation here? I don't even see square brackets.

> [@user16251](#):
>
> I was just trying to emphasize that having succinct notation to convert `bool` to an integer is extremely important.

Is it really **that** important though?

---

<div class="post-metadata">

**Author:** ![user16251](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/user16251/32/10186_2.png) [@user16251](https://internals.rust-lang.org/u/user16251)\
**Post date:** [May 29, 2023, 5:11pm UTC](https://internals.rust-lang.org/t/addition-to-rusts-int/18879/13 "2023-05-29T17:11:47Z")

</div>

You don't have to write bracket notation with brackets. What I'm trying to say is that converting booleans to integers is an incredibly useful idiom. Some comments in this thread suggest that it is mathematically meaningless or that it was a mistake to even offer conversions with `as`.

---

<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:** [May 29, 2023, 8:02pm UTC](https://internals.rust-lang.org/t/addition-to-rusts-int/18879/14 "2023-05-29T20:02:37Z")

</div>

> [@toc](#):
>
> `my_int + my_bool.into()`

This doesn't actually compile because both `Add` and `Into` are implemented for multiple types here and the type system is not smart enough to figure out the unique type that is in both sets.

---

<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:** [August 27, 2023, 8:03pm UTC](https://internals.rust-lang.org/t/addition-to-rusts-int/18879/15 "2023-08-27T20:03:21Z")

</div>

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