### 4 stable releases

new 1.0.3 | Apr 17, 2019 |
---|---|

1.0.2 | Nov 14, 2018 |

1.0.1 | Sep 4, 2018 |

#**43** in No standard library

**80** downloads per month

**MIT**license

6KB

67 lines

# Number As

**Use** the trait

in the current scope to let all primitive number types have a `NumberAs`

method.`number_as`

`extern` `crate` number_as`;`
`use` `number_as``::`NumberAs`;`
`let` a`:` `i32` `=` `2``u16``.``number_as``(``)``;`
`assert_eq!``(``2``i32``,` a`)``;`
`assert_eq!``(``2``i32``,` `2``u16``.``number_as``(``)``)``;`
`assert_eq!``(``20``i32``,` `20.``6``.``number_as``(``)``)``;`

All implements for the

trait can be considered as a whole `NumberAs`**Primitive Number**. If you want to design an **add** function, which accepts all kinds of primitive numbers,
you can use the

trait as a trait bound.`Number`

`extern` `crate` number_as`;`
`use` `number_as``::``{`Number`,` NumberAs`}``;`
`fn` `add``<`T, J, K`>``(``a``:` T, `b``:` J`)`` ``->` K `where` T`:` Number, J`:` Number, K`:` Number, i128`:` `NumberAs``<`K`>` `{`
`let` a`:` i128 `=` a`.``number_as``(``)``;`
`let` b`:` i128 `=` b`.``number_as``(``)``;`
`let` c `=` a `+` b`;`
c`.``number_as``(``)`
`}`
`assert_eq!``(``40``i32``,` `add``(``5``u8``,` `35``i16``)``)``;`

## Crates.io

https://crates.io/crates/number-as