Rust Primitive Type

###Rustの基本データ型の勉強ノート（毎日更新）
####整数のMINとMAX
- 符号付き整数: i8, i16, i32, i64, i128, isize（ポインタのサイズ）
- 符号無し整数: u8, i16, u32, u64, u128, usize（ポインタのサイズ）

```rs
#![allow(unused)]
fn main() {
    println!("  u8の値の範囲は{:?}～{:?}",u8::MIN,u8::MAX);
    println!(" u16の値の範囲は{:?}～{:?}",u16::MIN,u16::MAX);
    println!(" u32の値の範囲は{:?}～{:?}",u32::MIN,u32::MAX);
    println!(" u64の値の範囲は{:?}～{:?}",u64::MIN,u64::MAX);
    println!("usizeの値の範囲は{:?}～{:?}",usize::MIN,usize::MAX);
    println!("u128の値の範囲は{:?}～{:?}",u128::MIN,u128::MAX);
    println!("  i8の値の範囲は{:?}～{:?}",i8::MIN,i8::MAX);
    println!(" i16の値の範囲は{:?}～{:?}",i16::MIN,i16::MAX);
    println!(" i32の値の範囲は{:?}～{:?}",i32::MIN,i32::MAX);
    println!(" i64の値の範囲は{:?}～{:?}",i64::MIN,i64::MAX);
    println!("isizeの値の範囲は{:?}～{:?}",isize::MIN,isize::MAX);
    println!("i128の値の範囲は{:?}～{:?}",i128::MIN,i128::MAX);
}
```
- 実行した結果  
![MIN_MAX.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/551056/8b135e1a-300e-f2b6-9a1d-2b93a0a4a2c5.png)  
####整数の`from_str_radix`関数  
`pub fn from_str_radix(src: &str, radix: u32) -> Result<usize, ParseIntError>`

```rs
#![allow(unused)]
fn main() {
    println!("{:?}",i8::from_str_radix("a",16));
    println!("{:?}",i8::from_str_radix("-a",16));
    println!("{:?}",i8::from_str_radix("1111",2));
    println!("{:?}",i8::from_str_radix("ff",16)); //i8の範囲を超えた
    println!("{:?}",u8::from_str_radix("ff",16)); //u8は正常に転換できる
    println!("{:?}",i8::from_str_radix("4",3)); //3進数の許容数字範囲を超えた
}
```
- 実行した結果  
![from_str_radix.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/551056/776bcf61-d3c9-d24e-9298-7aaaba0da189.png)  

####整数の`pub const fn count_ones(self) -> u32`関数
２進数中に１の数を数える

```rs
#![allow(unused)]
fn main() {
    let n = 0xffi32;
    println!("{:?}", usize::from_str_radix("ff", 16));
    println!("{:?}", usize::from_str_radix("11111111", 2));
    println!("{:?}", n.count_ones()); //任意整数の２進数に１の数を数える
}
```
- 実行した結果  
![count_ones.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/551056/81db21e0-a3b6-a359-38f4-ed04bb67f9e6.png)

####整数の`pub const fn count_zeros(self) -> u32`関数
２進数中に０の数を数える

```rs
#![allow(unused)]
fn main() {
    let n = 0b1010i8;
    println!("{:?}", i8::from_str_radix("1010",2)); // 1010 と 0000_1010は同じです。
    println!("{:?}", n.count_zeros());
}
```
- 実行した結果  
![count_zeros.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/551056/752e702c-1cb1-b350-8c60-030c53e9fe47.png)

####整数の`pub const fn leading_zeros(self) -> u32`関数と`pub const fn trailing_zeros(self) -> u32`関数
２進数中に先頭（末尾）のゼロの数を数える

```rs
#![allow(unused)]
fn main() {
    let n = 0b00001010i8;
    println!("{:?}", i8::from_str_radix("1010",2)); // 1010 と 0000_1010は同じです。
    println!("leading_zeros: {:?}", n.leading_zeros());
    println!("trailing_zeros: {:?}", n.trailing_zeros());
}
```
- 実行した結果  
![leading_zeros.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/551056/db624f2b-8448-d8db-f3f2-8b1550f19eb8.png)

####整数の`pub fn leading_ones(self) -> u32`関数と`pub fn trailing_ones(self) -> u32`関数
２進数中に先頭（末尾）の１の数を数える

```rs
#![allow(unused)]
#![feature(leading_trailing_ones)]
fn main() {
    let n = -1i8;
    println!("{:?}",n.leading_ones());
    let n = 3i8;
    println!("{:?}",n.trailing_ones());
}
```
- 実行した結果  
![leading_ones.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/551056/c323b67a-4af0-1065-a13e-bd81e0e0c754.png)

####整数の`pub const fn rotate_left(self, n: u32) -> u8`関数と`pub const fn rotate_right(self, n: u32) -> u8`関数

```rs
#![allow(unused)]
fn main() {
    // rotate_left
    let n = 0b11011u8;
    println!("{:?}",u8::from_str_radix("11011",2));
    println!("{:?}",n.rotate_left(1)); //全体的に左へ１ビット移動する、右の１ビットは０で補足する
    println!("{:?}",u8::from_str_radix("110110",2));
    println!("{:?}",n.rotate_left(2)); //全体的に左へ２ビット移動する、右の２ビットは０で補足する
    println!("{:?}",u8::from_str_radix("1101100",2));
    println!("{:?}",n.rotate_left(3)); //全体的に左へ３ビット移動する、右の３ビットは０で補足する
    println!("{:?}",u8::from_str_radix("11011000",2));
    println!("{:?}",n.rotate_left(4)); //全体的に左へ４ビット移動する、右の４ビットは０で補足する（110110000）
    // 110110000全部は９桁になるため、溢れた１桁目の１は一番右の１桁に移動する10110001になる
    println!("{:?}",u8::from_str_radix("10110001",2));

    // rotate_right
    let n = 0b11011u8;
    println!("{:?}",u8::from_str_radix("11011",2));
    println!("{:?}",n.rotate_right(1));//全体的に右へ１ビット移動する、一番右の１が溢れたため左の１桁目に設定する
    println!("{:?}",u8::from_str_radix("10001101",2));
    println!("{:?}",n.rotate_right(2));
    println!("{:?}",u8::from_str_radix("11000110",2));
    println!("{:?}",n.rotate_right(3));
    println!("{:?}",u8::from_str_radix("01100011",2));
    println!("{:?}",n.rotate_right(4));
    println!("{:?}",u8::from_str_radix("10110001",2));
}
```
- 実行した結果（rotate_left）  
![rotate_left.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/551056/9393eb8f-2e3f-ce0d-ddd6-8a6505d5dc82.png)
- 実行した結果（rotate_right）  
![rotate_right.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/551056/f9ee3ca6-ce3c-920c-467c-ca548448308b.png)

####整数の`pub const fn swap_bytes(self) -> u8`関数と`pub const fn swap_bytes(self) -> u16`関数と`pub const fn swap_bytes(self) -> u32`関数
バイト（８ビット）単位で交換する

```rs
#![allow(unused)]
fn main() {
    // swap_bytes -> 1 byte
    let n = 0xfu8;
    println!("swap 1 byte: {:?}",u8::from_str_radix("f",16));
    println!("swap 1 byte: {:?}",n.swap_bytes());//0000_1111

    // swap_bytes -> 2 byte
    let n = 0xfu16;
    println!("swap 2 bytes: {:?}",u16::from_str_radix("f",16));
    println!("swap 2 bytes: {:?}",n.swap_bytes());//00000000_00001111 -> 00001111_00000000
    println!("swap 2 bytes: {:?}",u16::from_str_radix("0000111100000000",2));

    // swap_bytes -> 4 byte
    let n = 0xf00fu32;
    println!("swap 4 bytes: {:?}",u32::from_str_radix("f00f",16));
    //00000000_00000000_11110000_00001111 -> 00001111_11110000_00000000_00000000
    println!("swap 4 bytes: {:?}",n.swap_bytes());
    println!("swap 4 bytes: {:?}",u32::from_str_radix("00001111111100000000000000000000",2));
}
```
- 実行した結果  
![swap_bytes.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/551056/040f1114-c25e-0990-0bb8-c22805a22dd3.png)

####整数の`pub const fn reverse_bits(self) -> u32`関数
２進数の右から左へビットごとに並ぶ順を直すこと

```rs
#![allow(unused)]
fn main() {
    // reverse_bits -> 1 byte
    let n = 0xfu8;
    println!("reverse 1 byte: {:?}", u8::from_str_radix("f", 16));
    println!("reverse 1 byte: {:?}", n.reverse_bits()); //0000_1111 -> 1111_0000
    println!("reverse 1 byte: {:?}", u16::from_str_radix("11110000", 2));

    // reverse_bits -> 2 byte
    let n = 0xfu16;
    println!("reverse 2 bytes: {:?}", u16::from_str_radix("f", 16));
    println!("reverse 2 bytes: {:?}", n.reverse_bits()); //00000000_00001111 -> 11110000_00000000
    println!(
        "reverse 2 bytes: {:?}",
        u16::from_str_radix("1111000000000000", 2)
    );

    // reverse_bits -> 4 byte
    let n = 0xf00fu32;
    println!("reverse 4 bytes: {:?}", u32::from_str_radix("f00f", 16));
    //00000000_00000000_11110000_00001111 -> 11110000_00001111_00000000_00000000
    println!("reverse 4 bytes: {:?}", n.reverse_bits());
    println!(
        "reverse 4 bytes: {:?}",
        u32::from_str_radix("11110000000011110000000000000000", 2)
    );
}
```
- 実行した結果  
![reverse_bits.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/551056/2f8f30ec-51a1-94af-a7fb-37c14990d855.png)

####整数の`pub const fn from_be(x: u16) -> u16`関数と`pub const fn from_le(x: u16) -> u16`関数と`pub const fn to_be(self) -> u16`関数と`pub const fn to_le(self) -> u16`関数
`target_endian = "..." - Endianness of the target CPU, either "little" or "big".`

```rs
#![allow(unused)]
fn main() {
    let n = 0xAu16;
    println!("swap_bytes: {:?}",n.swap_bytes());//00000000_00001010 -> 00001010_00000000
    println!("swap_bytes: {:?}", u16::from_str_radix("0000101000000000", 2));

    let n = 0xAu16;
    if cfg!(target_endian = "big") {
        println!("from_be_big: {:?}",u16::from_be(n));
    } else {
        println!("from_be_little: {:?}",n.swap_bytes()); //この分岐が実行される
    }

    let n = 0xAu16;
    if cfg!(target_endian = "little") {
        println!("from_le_little: {:?}",u16::from_le(n)); //この分岐が実行される
    } else {
        println!("from_le_big: {:?}",n.swap_bytes());
    }

    let n = 0xAu16;
    if cfg!(target_endian = "big") {
        println!("to_be_big: {:?}",u16::to_be(n));
    } else {
        println!("to_be_little: {:?}",n.swap_bytes()); //この分岐が実行される
    }

    let n = 0xAu16;
    if cfg!(target_endian = "little") {
        println!("to_le_little: {:?}",u16::to_le(n)); //この分岐が実行される
    } else {
        println!("to_le_big: {:?}",n.swap_bytes());
    }
}
```
- 実行した結果  
![frombe_tole.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/551056/112f4d26-6dc3-22bc-4078-1cbe1421824d.png)

####整数の`pub fn checked_add(self, rhs: u16) -> Option<u16>`関数
checked_sub; checked_mul; checked_div; checked_div_euclid; checked_rem; checked_rem_euclid; checked_neg; checked_shl; checked_shr; checked_pow;

```rs
#![allow(unused)]
fn main() {
    //checked_add
    println!("checked_add_some: {:?}", (u16::MAX - 2).checked_add(1));
    println!("checked_add_None: {:?}", (u16::MAX - 2).checked_add(3));
    println!();

    //checked_sub
    println!("checked_sub_some: {:?}", 1u16.checked_sub(1));
    println!("checked_sub_None: {:?}", 0u16.checked_sub(1));
    println!();

    //checked_mul
    println!("checked_mul_some: {:?}", 5u16.checked_mul(1));
    println!("checked_mul_None: {:?}", u16::MAX.checked_mul(2));
    println!();

    //checked_div
    println!("checked_div_some: {:?}", 128u16.checked_div(2));
    println!("checked_div_None: {:?}", 1u16.checked_div(0));
    println!();

    //checked_div_euclid
    println!(
        "checked_div_euclid_some: {:?}",
        128u16.checked_div_euclid(2)
    );
    println!("checked_div_euclid_None: {:?}", 1u16.checked_div_euclid(0));
    println!();

    //checked_rem
    println!("checked_rem_some: {:?}", 5u16.checked_rem(2));
    println!("checked_rem_None: {:?}", 5u16.checked_rem(0));
    println!();

    //checked_rem_euclid
    println!("checked_rem_euclid_some: {:?}", 5u16.checked_rem_euclid(2));
    println!("checked_rem_euclid_None: {:?}", 5u16.checked_rem_euclid(0));
    println!();

    //checked_neg
    println!("checked_neg_some: {:?}", 0i16.checked_neg());
    println!("checked_neg_None: {:?}", 1u16.checked_neg());
    println!();

    //checked_shl
    println!("checked_shl_some: {:?}", 0x1u16.checked_shl(4));//00000001 -> 00010000
    println!("checked_shl_None: {:?}", 0x10u16.checked_shl(129));
    println!();

    //checked_shr
    println!("checked_shr_some: {:?}", 0x10u16.checked_shr(4));//00010000 -> 00000001
    println!("checked_shr_None: {:?}", 0x10u16.checked_shr(129));
    println!();

    //checked_pow
    println!("checked_pow_some: {:?}", 2u16.checked_pow(5));
    println!("checked_pow_None: {:?}", u16::MAX.checked_pow(2));
    println!();
}
```
- 実行した結果  
![checked_add.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/551056/8222027c-214e-8c15-1cde-da72d4291157.png)

####整数の`pub fn saturating_add(self, rhs: u16) -> u16`関数
saturating_sub; saturating_mul; saturating_pow;

```rs
#![allow(unused)]
fn main() {
    //saturating_add
    println!("saturating_add_some: {:?}", 100u16.saturating_add(1));
    println!("saturating_add_None: {:?}", 200u8.saturating_add(127)); //overflow -> u8::MAX
    println!();

    //saturating_sub
    println!("saturating_sub_some: {:?}", 100u16.saturating_sub(27));
    println!("saturating_sub_None: {:?}", 13u16.saturating_sub(127)); //overflow -> u16::MIN
    println!();

    //saturating_mul
    println!("saturating_mul_some: {:?}", 2u16.saturating_mul(10));
    println!("saturating_mul_None: {:?}", (u16::MAX).saturating_mul(10)); //overflow -> u16::MAX
    println!();

    //saturating_pow
    println!("saturating_pow_some: {:?}", 4u16.saturating_pow(3));
    println!("saturating_pow_None: {:?}", u16::MAX.saturating_pow(2)); //overflow -> u16::MAX
    println!();
}
```
- 実行した結果  
![saturating_add.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/551056/876a1465-2e28-c357-7818-22a6f2e99d4e.png)

####整数の`pub const fn wrapping_add(self, rhs: u16) -> u16`関数
wrapping_sub; wrapping_mul; wrapping_div; wrapping_div_euclid; wrapping_rem; wrapping_rem_euclid; wrapping_neg; wrapping_shl; wrapping_shr; wrapping_pow;

```rs
#![allow(unused)]
fn main() {
    //wrapping_add
    println!("wrapping_add: {:?}", 200u16.wrapping_add(55));
    println!("wrapping_add: {:?}", 200u16.wrapping_add(u16::MAX));
    println!();

    //wrapping_sub
    println!("wrapping_sub: {:?}", 100u16.wrapping_sub(100));
    println!("wrapping_sub: {:?}", 100u16.wrapping_sub(u16::MAX));
    println!();

    //wrapping_mul
    println!("wrapping_mul: {:?}", 10u8.wrapping_mul(12));
    println!("wrapping_mul: {:?}", 25u8.wrapping_mul(12));
    println!();

    //wrapping_div
    println!("wrapping_div: {:?}", 100u16.wrapping_div(10));
    println!();

    //wrapping_div_euclid
    println!("wrapping_div_euclid: {:?}", 100u16.wrapping_div_euclid(10));
    println!();

    //wrapping_rem
    println!("wrapping_rem: {:?}", 100u16.wrapping_rem(10));
    println!();

    //wrapping_rem_euclid
    println!("wrapping_rem_euclid: {:?}", 100u16.wrapping_rem_euclid(10));
    println!();

    //wrapping_neg
    println!("wrapping_neg: {:?}", 100i8.wrapping_neg());
    println!("wrapping_neg: {:?}", (-128i8).wrapping_neg());
    println!();

    //wrapping_shl
    println!("wrapping_shl: {:?}", 1u16.wrapping_shl(7));
    println!("wrapping_shl: {:?}", 1u16.wrapping_shl(128));
    println!();

    //wrapping_shr
    println!("wrapping_shr: {:?}", 128u16.wrapping_shr(7));
    println!("wrapping_shr: {:?}", 128u16.wrapping_shr(128));
    println!();

    //wrapping_pow
    println!("wrapping_pow: {:?}", 3u16.wrapping_pow(5));
    println!("wrapping_pow: {:?}", 3u8.wrapping_pow(6));
    println!();
}
```
- 実行した結果  
![wrapping_add.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/551056/b4f14207-3ba8-4c95-18cc-86faf970c9e8.png)


####整数の`pub const fn overflowing_add(self, rhs: u16) -> (u16, bool)`関数
overflowing_sub; overflowing_mul; overflowing_div; overflowing_div_euclid; overflowing_rem; overflowing_rem_euclid; overflowing_neg; overflowing_shl; overflowing_shr; overflowing_pow;

```rs
#![allow(unused)]
fn main() {
    //overflowing_add
    println!("overflowing_add: {:?}", 5u16.overflowing_add(2));
    println!("overflowing_add: {:?}", u16::MAX.overflowing_add(1));
    println!();

    //overflowing_sub
    println!("overflowing_sub: {:?}", 5u16.overflowing_sub(2));
    println!("overflowing_sub: {:?}", 0u16.overflowing_sub(1));
    println!();

    //overflowing_mul
    println!("overflowing_mul: {:?}", 5u32.overflowing_mul(2));
    println!("overflowing_mul: {:?}", 1_000_000_000u32.overflowing_mul(10));
    println!();

    //overflowing_div
    println!("overflowing_div: {:?}", 5u16.overflowing_div(2));
    println!();

    //overflowing_div_euclid
    println!(
        "overflowing_div_euclid: {:?}",
        5u16.overflowing_div_euclid(2)
    );
    println!();

    //overflowing_rem
    println!("overflowing_rem: {:?}", 5u16.overflowing_rem(2));
    println!();

    //overflowing_rem_euclid
    println!(
        "overflowing_rem_euclid: {:?}",
        5u16.overflowing_rem_euclid(2)
    );
    println!();

    //overflowing_neg
    println!("overflowing_neg: {:?}", 0u16.overflowing_neg());
    println!("overflowing_neg: {:?}", 2u16.overflowing_neg());
    println!();

    //overflowing_shl
    println!("overflowing_shl: {:?}", 0x1u16.overflowing_shl(4));
    println!("overflowing_shl: {:?}", 0x1u16.overflowing_shl(132));
    println!();

    //overflowing_shr
    println!("overflowing_shr: {:?}", 0x10u16.overflowing_shr(4));
    println!("overflowing_shr: {:?}", 0x10u16.overflowing_shr(132));
    println!();

    //overflowing_pow
    println!("overflowing_pow: {:?}", 3u16.overflowing_pow(5));
    println!("overflowing_pow: {:?}", 3u8.overflowing_pow(6));
    println!();
}
```
- 実行した結果  
![overflowing_add.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/551056/ddedf530-d20f-c231-f1ae-be1da75b7d96.png)