diff --git a/solutions/basic-types/numbers.md b/solutions/basic-types/numbers.md index e69de29..b622233 100644 --- a/solutions/basic-types/numbers.md +++ b/solutions/basic-types/numbers.md @@ -0,0 +1,132 @@ +1. +```rust +fn main() { + let x: i32 = 5; + let mut y = 5; + + y = x; + + let z = 10; // type of z : i32 +} +``` + +2. +```rust +fn main() { + let v: u16 = 38_u8 as u16; +} +``` + +3. +```rust +fn main() { + let x = 5; + assert_eq!("i32".to_string(), type_of(&x)); +} + +// get the type of given variable, return a string representation of the type , e.g "i8", "u8", "i32", "u32" +fn type_of(_: &T) -> String { + format!("{}", std::any::type_name::()) +} +``` + +4. +```rust +fn main() { + assert_eq!(i8::MAX, 127); + assert_eq!(u8::MAX, 255); +} +``` + +5. +```rust +fn main() { + let v1 = 247_u8 + 8; + let v2 = i8::checked_add(119, 8).unwrap(); + println!("{},{}",v1,v2); + } + ``` + +6. + ```rust + fn main() { + let v = 1_024 + 0xff + 0o77 + 0b1111_1111; + assert!(v == 1597); +} + ``` + +7. + ```rust + fn main() { + let x = 1_000.000_1; // f64 + let y: f32 = 0.12; // f32 + let z = 0.01_f64; // f64 +} + ``` + +8. +```rust +fn main() { + assert!(0.1_f32+0.2_f32==0.3_f32); +} +``` + +```rust +fn main() { + assert!((0.1_f64+ 0.2 - 0.3).abs() < 0.001); +} +``` + +9. +```rust +fn main() { + let mut sum = 0; + for i in -3..2 { + sum += i + } + + assert!(sum == -5); + + for c in 'a'..='z' { + println!("{}",c as u8); + } +} +``` + +10. +```rust +use std::ops::{Range, RangeInclusive}; +fn main() { + assert_eq!((1..5), Range{ start: 1, end: 5 }); + assert_eq!((1..=5), RangeInclusive::new(1, 5)); +} +``` + +11. +```rust +fn main() { + // Integer addition + assert!(1u32 + 2 == 3); + + // Integer subtraction + assert!(1i32 - 2 == -1); + assert!(1i8 - 2 == -1); + + assert!(3 * 50 == 150); + + assert!(9 / 3 == 3); // error ! make it work + + assert!(24 % 5 == 4); + // Short-circuiting boolean logic + assert!(true && false == false); + assert!(true || false == true); + assert!(!true == false); + + // Bitwise operations + println!("0011 AND 0101 is {:04b}", 0b0011u32 & 0b0101); + println!("0011 OR 0101 is {:04b}", 0b0011u32 | 0b0101); + println!("0011 XOR 0101 is {:04b}", 0b0011u32 ^ 0b0101); + println!("1 << 5 is {}", 1u32 << 5); + println!("0x80 >> 2 is 0x{:x}", 0x80u32 >> 2); +} +``` \ No newline at end of file diff --git a/src/basic-types/numbers.md b/src/basic-types/numbers.md index 2c86954..0dd69ba 100644 --- a/src/basic-types/numbers.md +++ b/src/basic-types/numbers.md @@ -2,7 +2,7 @@ ### Integer -๐ŸŒŸ +1. ๐ŸŒŸ > Tips: If we don't explicitly give one type to a varible, then the compiler will infer one for us @@ -19,7 +19,7 @@ fn main() { } ``` -๐ŸŒŸ +2. ๐ŸŒŸ ```rust,editable // fill the blank @@ -28,7 +28,7 @@ fn main() { } ``` -๐ŸŒŸ๐ŸŒŸ๐ŸŒŸ +3. ๐ŸŒŸ๐ŸŒŸ๐ŸŒŸ > Tips: If we don't explicitly give one type to a varible, then the compiler will infer one for us @@ -46,7 +46,7 @@ fn type_of(_: &T) -> String { } ``` -๐ŸŒŸ๐ŸŒŸ +4. ๐ŸŒŸ๐ŸŒŸ ```rust,editable // fill the blanks to make it work @@ -56,7 +56,7 @@ fn main() { } ``` -๐ŸŒŸ๐ŸŒŸ +5. ๐ŸŒŸ๐ŸŒŸ ```rust,editable // fix errors and panics to make it work @@ -67,7 +67,7 @@ fn main() { } ``` -๐ŸŒŸ๐ŸŒŸ๐ŸŒŸ +6. ๐ŸŒŸ๐ŸŒŸ ```rust,editable // modify `assert!` to make it work @@ -79,7 +79,7 @@ fn main() { ### Floating-Point -๐ŸŒŸ +7. ๐ŸŒŸ ```rust,editable @@ -90,9 +90,8 @@ fn main() { let z = 0.01_f64; // f64 } ``` -๐ŸŒŸ๐ŸŒŸ๐ŸŒŸ use two ways to make it work -> Tips: 1. abs 2. f32 +8. ๐ŸŒŸ๐ŸŒŸ use two ways to make it work ```rust,editable @@ -102,9 +101,8 @@ fn main() { ``` ### Range -๐ŸŒŸ๐ŸŒŸ two targets: 1. modify `assert!` to make it work 2. make `println!` output: 97 - 122 +9. ๐ŸŒŸ๐ŸŒŸ two targets: 1. modify `assert!` to make it work 2. make `println!` output: 97 - 122 -> Tips: use `as u8` to convert a char to u8 ```rust,editable fn main() { let mut sum = 0; @@ -114,13 +112,13 @@ fn main() { assert!(sum == -3); - for c in 'a'..='Z' { + for c in 'a'..='z' { println!("{}",c); } } ``` -๐ŸŒŸ๐ŸŒŸ +10. ๐ŸŒŸ๐ŸŒŸ ```rust,editable // fill the blanks @@ -133,17 +131,17 @@ fn main() { ### Computations -๐ŸŒŸ +11. ๐ŸŒŸ ```rust,editable -// fill the blanks +// fill the blanks and fix the errors fn main() { // Integer addition assert!(1u32 + 2 == __); // Integer subtraction assert!(1i32 - 2 == __); - assert!(1u8 - 2 == -1); // change u8 to another type to make it work + assert!(1u8 - 2 == -1); assert!(3 * 50 == __); diff --git a/zh-CN/src/basic-types/numbers.md b/zh-CN/src/basic-types/numbers.md index 9eee641..1719668 100644 --- a/zh-CN/src/basic-types/numbers.md +++ b/zh-CN/src/basic-types/numbers.md @@ -67,7 +67,7 @@ fn main() { } ``` -๐ŸŒŸ๐ŸŒŸ๐ŸŒŸ +๐ŸŒŸ๐ŸŒŸ ```rust,editable // ไฟฎๆ”น `assert!` ่ฎฉไปฃ็ ๅทฅไฝœ @@ -90,9 +90,8 @@ fn main() { let z = 0.01_f64; // f64 } ``` -๐ŸŒŸ๐ŸŒŸ๐ŸŒŸ ไฝฟ็”จไธค็งๆ–นๆณ•ๆฅ่ฎฉไธ‹้ขไปฃ็ ๅทฅไฝœ +๐ŸŒŸ๐ŸŒŸ ไฝฟ็”จไธค็งๆ–นๆณ•ๆฅ่ฎฉไธ‹้ขไปฃ็ ๅทฅไฝœ -> ๆ็คบ: 1. abs 2. f32 ```rust,editable @@ -104,7 +103,6 @@ fn main() { ### ๅบๅˆ—Range ๐ŸŒŸ๐ŸŒŸ ไธคไธช็›ฎๆ ‡: 1. ไฟฎๆ”น `assert!` ่ฎฉๅฎƒๅทฅไฝœ 2. ่ฎฉ `println!` ่พ“ๅ‡บ: 97 - 122 -> Tips: ไฝฟ็”จ `as u8` ๅฐ†ๅญ—็ฌฆ `char` ่ฝฌๆขๆˆ `u8` ็ฑปๅž‹ ```rust,editable fn main() { let mut sum = 0; @@ -114,7 +112,7 @@ fn main() { assert!(sum == -3); - for c in 'a'..='Z' { + for c in 'a'..='z' { println!("{}",c); } } @@ -136,14 +134,14 @@ fn main() { ๐ŸŒŸ ```rust,editable -// ๅกซ็ฉบ +// ๅกซ็ฉบ๏ผŒๅนถ่งฃๅ†ณ้”™่ฏฏ fn main() { // ๆ•ดๆ•ฐๅŠ ๆณ• assert!(1u32 + 2 == __); // ๆ•ดๆ•ฐๅ‡ๆณ• assert!(1i32 - 2 == __); - assert!(1u8 - 2 == -1); // ๅฐ† u8 ไฟฎๆ”นไธบๅฆไธ€ไธช็ฑปๅž‹ๆฅ่ฎฉไปฃ็ ๅทฅไฝœ + assert!(1u8 - 2 == -1); assert!(3 * 50 == __); assert!(9.6 / 3.2 == 3.0); // error ! ไฟฎๆ”นๅฎƒ่ฎฉไปฃ็ ๅทฅไฝœ