# Pre-RFC: impl From\<bool\> for i32 and impl From\<i32\> for bool

**URL:** <https://internals.rust-lang.org/t/pre-rfc-impl-from-bool-for-i32-and-impl-from-i32-for-bool/3168>\
**Category:** libs\
**Created:** [February 10, 2016, 5:42pm UTC](https://internals.rust-lang.org/t/pre-rfc-impl-from-bool-for-i32-and-impl-from-i32-for-bool/3168 "2016-02-10T17:42:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nodakai](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nodakai/32/6161_2.png) [@nodakai](https://internals.rust-lang.org/u/nodakai)\
**Post date:** [February 10, 2016, 5:42pm UTC](https://internals.rust-lang.org/t/pre-rfc-impl-from-bool-for-i32-and-impl-from-i32-for-bool/3168/1 "2016-02-10T17:42:55Z")

</div>

How do you like

```
let b: bool = 123_i32.into();
let i: i32 = true.into();

```

compared to

```
let b = 123_i32 != 0; // no "as bool" in the language
let i = true as i32;

```

My position is to take every possible action to reduce the uses of the `as` keyword which looks to my eyes too error-prone. So `impl From<i32> for bool` is just a bonus 😉

Obviously this can be extended to `impl From<bool> for i8` etc etc.

Slightly related: `impl From<i16> for isize` etc:

> <https://github.com/rust-lang/rust/pull/29220>

Update: perhaps more importantly, this is the PR to be extended by my proposal:

[https://github.com/rust-lang/rust/pull/28921](https://github.com/rust-lang/rust/pull/28921)

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [February 10, 2016, 6:06pm UTC](https://internals.rust-lang.org/t/pre-rfc-impl-from-bool-for-i32-and-impl-from-i32-for-bool/3168/2 "2016-02-10T18:06:24Z")

</div>

Don’t add `From` impls if you don’t think they make for good argument conversions.

As in[(playpen)](http://is.gd/nd7fsz)

```rust
fn absolute_32_bit<T>(a: T) -> i32 
    where T: Into<i32>
{
    a.into().abs()
}

```

Every `From` turns into something that can be used with `Into`, and that is an argument conversion trait. I admit, I don’t see much realistic use for `Into<i32>` either. But should this function for example accept booleans automatically? I can see it sort of make sense for i8 and i16, but not bool.

---

<div class="post-metadata">

**Author:** ![briansmith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/briansmith/32/1306_2.png) [@briansmith](https://internals.rust-lang.org/u/briansmith)\
**Post date:** [February 10, 2016, 6:25pm UTC](https://internals.rust-lang.org/t/pre-rfc-impl-from-bool-for-i32-and-impl-from-i32-for-bool/3168/3 "2016-02-10T18:25:46Z")

</div>

> [@nodakai](#):
>
> My position is to take every possible action to reduce the uses of the as keyword which looks to my eyes too error-prone.

I agree that we should be working towards deprecating and removing the `as` casting feature as it is error-prone. However, I don't think implementing these `From` instances should be part of that. I would say that `let b = 123_i32 != 0` and `let i = if b { 1 } else { 0 }` are the best ways of doing the conversions you mention. Perhaps these should be put in the style guide.

`<bool> as <integer type>` and `<integer type> as bool` should just be deprecated straight away.

---

<div class="post-metadata">

**Author:** ![nodakai](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nodakai/32/6161_2.png) [@nodakai](https://internals.rust-lang.org/u/nodakai)\
**Post date:** [February 10, 2016, 7:26pm UTC](https://internals.rust-lang.org/t/pre-rfc-impl-from-bool-for-i32-and-impl-from-i32-for-bool/3168/4 "2016-02-10T19:26:14Z")

</div>

Guys, why do you discriminate against our little `i1` (one-bit integral) type? 😒 So its "absolute value" might as well also be defined...

> [@briansmith](#):
>
> ... and `let i = if b { 1 } else { 0 }` are the best ways

Well, this might be arguable...

> [@briansmith](#):
>
> `<bool>` as `<integer type>` and `<integer type>` as `bool` should just be deprecated straight away.

Thankfully, the latter is already a hard error:

> **[Rust error codes index - Error codes index](https://doc.rust-lang.org/error-index.html#E0054)**
>
> Book listing all Rust error codes

---

<div class="post-metadata">

**Author:** ![DanielKeep](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danielkeep/32/1332_2.png) [@DanielKeep](https://internals.rust-lang.org/u/DanielKeep)\
**Post date:** [February 11, 2016, 2:47am UTC](https://internals.rust-lang.org/t/pre-rfc-impl-from-bool-for-i32-and-impl-from-i32-for-bool/3168/5 "2016-02-11T02:47:46Z")

</div>

> [@nodakai](#):
>
> Guys, why do you discriminate against our little i1 (one-bit integral) type?

Because it isn't a one-bit integral type. It's a boolean logic value. It's a completely different domain.

I mean, you could argue that every type in Rust is just [i1; N] for some `N`, so _obviously_ we should be able to slice a `String` and get an `f32`. But that would defeat the point of having a strong type system in the first place.

`1` is no more "true" than `4`, or `17`, or `9275` or `-8`, or `"banana"` or `vec!["coconuts", "coconuts"]` or even `0`. Don't forget that `0` is "true" in the UNIX shell.

_"But what about the existence of `Into<Vec<u8>> for String`?"_

... shut up, me.

---

<div class="post-metadata">

**Author:** ![briansmith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/briansmith/32/1306_2.png) [@briansmith](https://internals.rust-lang.org/u/briansmith)\
**Post date:** [February 11, 2016, 4:56am UTC](https://internals.rust-lang.org/t/pre-rfc-impl-from-bool-for-i32-and-impl-from-i32-for-bool/3168/6 "2016-02-11T04:56:56Z")

</div>

> [@DanielKeep](#):
>
> "But what about the existence of Into\<Vec\<u8\>\> for String?"
> 
> ... shut up, me.

I agree, that is actually probably worse than any boolean conversions.

---

<div class="post-metadata">

**Author:** ![DanielKeep](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danielkeep/32/1332_2.png) [@DanielKeep](https://internals.rust-lang.org/u/DanielKeep)\
**Post date:** [February 11, 2016, 5:00am UTC](https://internals.rust-lang.org/t/pre-rfc-impl-from-bool-for-i32-and-impl-from-i32-for-bool/3168/7 "2016-02-11T05:00:31Z")

</div>

I think it’s a symptom of trying to cram every kind of non-primitive conversion into a single trait. One of the reasons I started `conv` is that you can’t really rely on what an `Into` or `From` conversion _means_. They basically appear to mean “there is a conversion of _some sort_ that will maybe succeed, that might or might not change representation and/or semantic meaning.”

In other words, they’re near completely useless in any generic context.

That said, I don’t want the situation to get _worse_.

---

<div class="post-metadata">

**Author:** ![briansmith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/briansmith/32/1306_2.png) [@briansmith](https://internals.rust-lang.org/u/briansmith)\
**Post date:** [February 11, 2016, 5:17am UTC](https://internals.rust-lang.org/t/pre-rfc-impl-from-bool-for-i32-and-impl-from-i32-for-bool/3168/8 "2016-02-11T05:17:49Z")

</div>

Daniel, you might enjoy this: [http://degoes.net/articles/principled-typeclasses/](http://degoes.net/articles/principled-typeclasses/)

---

<div class="post-metadata">

**Author:** ![leonardo](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/leonardo/32/1344_2.png) [@leonardo](https://internals.rust-lang.org/u/leonardo)\
**Post date:** [February 11, 2016, 2:55pm UTC](https://internals.rust-lang.org/t/pre-rfc-impl-from-bool-for-i32-and-impl-from-i32-for-bool/3168/9 "2016-02-11T14:55:51Z")

</div>

> [@briansmith](#):
>
> Daniel, you might enjoy this: [http://degoes.net/articles/principled-typeclasses/](http://degoes.net/articles/principled-typeclasses/)

This is interesting, but also confuses me a little. Are such ideas useful for future Rust developments?

---

<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:25am UTC](https://internals.rust-lang.org/t/pre-rfc-impl-from-bool-for-i32-and-impl-from-i32-for-bool/3168/10 "2019-03-25T08:25:47Z")

</div>

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