2022-02-23 00:49:02 -06:00
|
|
|
# Numbers
|
2022-02-24 06:31:19 -06:00
|
|
|
|
|
|
|
### Integer
|
|
|
|
|
2022-03-01 08:24:47 -06:00
|
|
|
1. π
|
2022-02-24 06:31:19 -06:00
|
|
|
|
|
|
|
> Tips: If we don't explicitly give one type to a varible, then the compiler will infer one for us
|
2022-02-28 03:27:33 -06:00
|
|
|
|
2022-02-24 06:31:19 -06:00
|
|
|
```rust,editable
|
|
|
|
|
2022-02-28 03:27:33 -06:00
|
|
|
// remove something to make it work
|
2022-02-24 06:31:19 -06:00
|
|
|
fn main() {
|
|
|
|
let x: i32 = 5;
|
|
|
|
let mut y: u32 = 5;
|
|
|
|
|
|
|
|
y = x;
|
|
|
|
|
|
|
|
let z = 10; // type of z ?
|
2022-03-05 00:48:43 -06:00
|
|
|
|
|
|
|
println!("Success!")
|
2022-02-24 06:31:19 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-03-01 08:24:47 -06:00
|
|
|
2. π
|
2022-02-24 06:53:40 -06:00
|
|
|
```rust,editable
|
|
|
|
|
2022-02-28 03:27:33 -06:00
|
|
|
// fill the blank
|
2022-02-24 06:53:40 -06:00
|
|
|
fn main() {
|
|
|
|
let v: u16 = 38_u8 as __;
|
2022-03-05 00:48:43 -06:00
|
|
|
|
|
|
|
println!("Success!")
|
2022-02-24 06:53:40 -06:00
|
|
|
}
|
|
|
|
```
|
2022-02-24 06:31:19 -06:00
|
|
|
|
2022-03-01 08:24:47 -06:00
|
|
|
3. πππ
|
2022-02-24 06:31:19 -06:00
|
|
|
|
|
|
|
> Tips: If we don't explicitly give one type to a varible, then the compiler will infer one for us
|
|
|
|
|
|
|
|
```rust,editable
|
|
|
|
|
2022-02-28 03:27:33 -06:00
|
|
|
// modify `assert_eq!` to make it work
|
2022-02-24 06:31:19 -06:00
|
|
|
fn main() {
|
|
|
|
let x = 5;
|
|
|
|
assert_eq!("u32".to_string(), type_of(&x));
|
2022-03-05 00:48:43 -06:00
|
|
|
|
|
|
|
println!("Success!")
|
2022-02-24 06:31:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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>())
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-03-01 08:24:47 -06:00
|
|
|
4. ππ
|
2022-02-24 06:31:19 -06:00
|
|
|
```rust,editable
|
|
|
|
|
2022-02-28 03:27:33 -06:00
|
|
|
// fill the blanks to make it work
|
2022-02-24 06:31:19 -06:00
|
|
|
fn main() {
|
|
|
|
assert_eq!(i8::MAX, __);
|
|
|
|
assert_eq!(u8::MAX, __);
|
2022-03-05 00:48:43 -06:00
|
|
|
|
|
|
|
println!("Success!")
|
2022-02-24 06:31:19 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-03-01 08:24:47 -06:00
|
|
|
5. ππ
|
2022-02-24 06:31:19 -06:00
|
|
|
```rust,editable
|
|
|
|
|
2022-02-28 03:27:33 -06:00
|
|
|
// fix errors and panics to make it work
|
2022-02-24 06:31:19 -06:00
|
|
|
fn main() {
|
|
|
|
let v1 = 251_u8 + 8;
|
|
|
|
let v2 = i8::checked_add(251, 8).unwrap();
|
|
|
|
println!("{},{}",v1,v2);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-03-01 08:24:47 -06:00
|
|
|
6. ππ
|
2022-02-24 06:31:19 -06:00
|
|
|
```rust,editable
|
|
|
|
|
2022-02-28 03:27:33 -06:00
|
|
|
// modify `assert!` to make it work
|
2022-02-24 06:31:19 -06:00
|
|
|
fn main() {
|
|
|
|
let v = 1_024 + 0xff + 0o77 + 0b1111_1111;
|
|
|
|
assert!(v == 1579);
|
2022-03-05 00:48:43 -06:00
|
|
|
|
|
|
|
println!("Success!")
|
2022-02-24 06:31:19 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-02-24 06:53:40 -06:00
|
|
|
|
2022-02-24 06:31:19 -06:00
|
|
|
### Floating-Point
|
2022-03-01 08:24:47 -06:00
|
|
|
7. π
|
2022-02-24 06:31:19 -06:00
|
|
|
|
|
|
|
```rust,editable
|
|
|
|
|
2022-02-28 03:27:33 -06:00
|
|
|
// replace ? with your answer
|
2022-02-24 06:31:19 -06:00
|
|
|
fn main() {
|
|
|
|
let x = 1_000.000_1; // ?
|
|
|
|
let y: f32 = 0.12; // f32
|
|
|
|
let z = 0.01_f64; // f64
|
2022-03-05 00:48:43 -06:00
|
|
|
|
|
|
|
println!("Success!")
|
2022-02-24 06:31:19 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-03-02 02:37:02 -06:00
|
|
|
1. ππ make it work in two distinct ways
|
2022-02-24 06:31:19 -06:00
|
|
|
|
|
|
|
```rust,editable
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
assert!(0.1+0.2==0.3);
|
2022-03-05 00:48:43 -06:00
|
|
|
|
|
|
|
println!("Success!")
|
2022-02-24 06:31:19 -06:00
|
|
|
}
|
2022-02-24 06:53:40 -06:00
|
|
|
```
|
|
|
|
|
|
|
|
### Range
|
2022-03-02 02:37:02 -06:00
|
|
|
9. ππ two goals: 1. modify `assert!` to make it work 2. make `println!` output: 97 - 122
|
2022-02-24 06:53:40 -06:00
|
|
|
|
|
|
|
```rust,editable
|
|
|
|
fn main() {
|
|
|
|
let mut sum = 0;
|
|
|
|
for i in -3..2 {
|
|
|
|
sum += i
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(sum == -3);
|
|
|
|
|
2022-03-01 08:24:47 -06:00
|
|
|
for c in 'a'..='z' {
|
2022-02-24 06:53:40 -06:00
|
|
|
println!("{}",c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-03-01 08:24:47 -06:00
|
|
|
10. ππ
|
2022-02-24 06:53:40 -06:00
|
|
|
```rust,editable
|
|
|
|
|
2022-02-28 03:27:33 -06:00
|
|
|
// fill the blanks
|
2022-02-24 06:53:40 -06:00
|
|
|
use std::ops::{Range, RangeInclusive};
|
|
|
|
fn main() {
|
|
|
|
assert_eq!((1..__), Range{ start: 1, end: 5 });
|
|
|
|
assert_eq!((1..__), RangeInclusive::new(1, 5));
|
2022-03-05 00:48:43 -06:00
|
|
|
|
|
|
|
println!("Success!")
|
2022-02-24 06:53:40 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Computations
|
|
|
|
|
2022-03-01 08:24:47 -06:00
|
|
|
11. π
|
2022-02-24 06:53:40 -06:00
|
|
|
```rust,editable
|
|
|
|
|
2022-03-01 08:24:47 -06:00
|
|
|
// fill the blanks and fix the errors
|
2022-02-24 06:53:40 -06:00
|
|
|
fn main() {
|
|
|
|
// Integer addition
|
|
|
|
assert!(1u32 + 2 == __);
|
|
|
|
|
|
|
|
// Integer subtraction
|
|
|
|
assert!(1i32 - 2 == __);
|
2022-03-01 08:24:47 -06:00
|
|
|
assert!(1u8 - 2 == -1);
|
2022-02-24 06:53:40 -06:00
|
|
|
|
|
|
|
assert!(3 * 50 == __);
|
|
|
|
|
|
|
|
assert!(9.6 / 3.2 == 3.0); // error ! make it work
|
|
|
|
|
|
|
|
assert!(24 % 5 == __);
|
|
|
|
// Short-circuiting boolean logic
|
|
|
|
assert!(true && false == __);
|
|
|
|
assert!(true || false == __);
|
|
|
|
assert!(!true == __);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
```
|
2022-03-01 08:06:38 -06:00
|
|
|
|
|
|
|
> You can find the solutions [here](https://github.com/sunface/rust-by-practice)(under the solutions path), but only use it when you need it
|