update numbers

This commit is contained in:
sunface 2022-02-24 20:31:19 +08:00
parent 660398d8d7
commit fc3bbc481c
9 changed files with 100 additions and 10 deletions

View File

@ -2,7 +2,7 @@
- [About Exercise.rs](why-exercise.md)
- [Variables](variables.md)
- [Basic Types todo](basic-types/intro.md)
- [Basic Types](basic-types/intro.md)
- [Numbers](basic-types/numbers.md)
- [Char, Bool and Unit](basic-types/char-bool-unit.md)
- [Statements and Expressions](basic-types/statements-expressions.md)

View File

@ -1 +1,5 @@
# Basic Types
Learning resources:
- English: [Rust Book 3.2 and 3.3](https://doc.rust-lang.org/book/ch03-02-data-types.html)
- 简体中文: [Rust语言圣经 - 基本类型](https://course.rs/basic/base-type/index.html)

View File

@ -1 +1,86 @@
# Numbers
### Integer
🌟 remove something to make it work
> Tips: If we don't explicitly give one type to a varible, then the compiler will infer one for us
```rust,editable
fn main() {
let x: i32 = 5;
let mut y: u32 = 5;
y = x;
let z = 10; // type of z ?
}
```
🌟🌟🌟 modify `assert_eq!` to make it work
> Tips: If we don't explicitly give one type to a varible, then the compiler will infer one for us
```rust,editable
fn main() {
let x = 5;
assert_eq!("u32".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>())
}
```
🌟🌟 fill the blanks to make it work
```rust,editable
fn main() {
assert_eq!(i8::MAX, __);
assert_eq!(u8::MAX, __);
}
```
🌟🌟 fix errors and panics to make it work
```rust,editable
fn main() {
let v1 = 251_u8 + 8;
let v2 = i8::checked_add(251, 8).unwrap();
println!("{},{}",v1,v2);
}
```
🌟🌟🌟 modify `assert!` to make it work
```rust,editable
fn main() {
let v = 1_024 + 0xff + 0o77 + 0b1111_1111;
assert!(v == 1579);
}
```
### Floating-Point
🌟 replace ? with your answer
```rust,editable
fn main() {
let x = 1_000.000_1; // ?
let y: f32 = 0.12; // f32
let z = 0.01_f64; // f64
}
```
🌟🌟🌟 use two ways to make it work
> Tips: 1. abs 2. f32
```rust,editable
fn main() {
assert!(0.1+0.2==0.3);
}
```

View File

@ -1,7 +1,7 @@
# Variables
### Binding and mutablity
🌟 fix the error below with least change
🌟 fix the error below with least modifying
```rust,editable
fn main() {
@ -24,7 +24,7 @@ fn main() {
```
### Scope
🌟 fix the error below with least change
🌟 fix the error below with least modifying
```rust,editable
fn main() {
@ -50,7 +50,7 @@ fn define_x() {
```
### Shadowing
🌟🌟 only change `assert_eq!` to make the `println!` work(print `42` in terminal)
🌟🌟 only modify `assert_eq!` to make the `println!` work(print `42` in terminal)
```rust,editable
@ -103,7 +103,7 @@ fn main() {
```
### Destructing
🌟🌟 fix the error below with least change
🌟🌟 fix the error below with least modifying
> Tips: you can use Shadowing or Mutability

View File

@ -2,7 +2,7 @@
- [关于 exercise.rs](why-exercise.md)
- [变量绑定与解构](variables.md)
- [基本类型 todo](basic-types/intro.md)
- [基本类型](basic-types/intro.md)
- [数值类型](basic-types/numbers.md)
- [字符、布尔、单元类型](basic-types/char-bool-unit.md)
- [语句与表达式](basic-types/statements-expressions.md)

View File

@ -1 +1,5 @@
# Basic Types
# 基本类型
学习资料:
- English: [Rust Book 3.2 and 3.3](https://doc.rust-lang.org/book/ch03-02-data-types.html)
- 简体中文: [Rust语言圣经 - 基本类型](https://course.rs/basic/base-type/index.html)

View File

@ -1 +0,0 @@
# Weak and Circle reference

View File

@ -1 +0,0 @@
# Crate

View File

@ -1 +0,0 @@
# panic!