add solutions for numbers
This commit is contained in:
parent
258f0ed589
commit
6be05a6686
|
@ -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>(_: &T) -> String {
|
||||
format!("{}", std::any::type_name::<T>())
|
||||
}
|
||||
```
|
||||
|
||||
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);
|
||||
}
|
||||
```
|
|
@ -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>(_: &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 == __);
|
||||
|
||||
|
|
|
@ -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 ! 修改它让代码工作
|
||||
|
|
Loading…
Reference in New Issue