update repo layout

This commit is contained in:
sunface
2022-03-23 20:04:00 +08:00
parent e2d3027b60
commit c1d8b06518
111 changed files with 958 additions and 4 deletions

View File

@ -0,0 +1,100 @@
# Char, Bool and Unit
### Char
1. 🌟
```rust, editable
// make it work
use std::mem::size_of_val;
fn main() {
let c1 = 'a';
assert_eq!(size_of_val(&c1),1);
let c2 = '中';
assert_eq!(size_of_val(&c2),3);
println!("Success!")
}
```
2. 🌟
```rust, editable
// make it work
fn main() {
let c1 = "中";
print_char(c1);
}
fn print_char(c : char) {
println!("{}", c);
}
```
### Bool
3. 🌟
```rust, editable
// make println! work
fn main() {
let _f: bool = false;
let t = true;
if !t {
println!("Success!")
}
}
```
4. 🌟
```rust, editable
// make it work
fn main() {
let f = true;
let t = true && false;
assert_eq!(t, f);
println!("Success!")
}
```
### Unit type
5. 🌟🌟
```rust,editable
// make it work, don't modify `implicitly_ret_unit` !
fn main() {
let _v: () = ();
let v = (2, 3);
assert_eq!(v, implicitly_ret_unit());
println!("Success!")
}
fn implicitly_ret_unit() {
println!("I will return a ()")
}
// don't use this one
fn explicitly_ret_unit() -> () {
println!("I will return a ()")
}
```
6. 🌟🌟 what's the size of the unit type?
```rust,editable
// modify `4` in assert to make it work
use std::mem::size_of_val;
fn main() {
let unit: () = ();
assert!(size_of_val(&unit) == 4);
println!("Success!")
}
```
> 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

View File

@ -0,0 +1,101 @@
# Functions
1. 🌟🌟🌟
```rust,editable
fn main() {
// don't modify the following two lines!
let (x, y) = (1, 2);
let s = sum(x, y);
assert_eq!(s, 3);
println!("Success!")
}
fn sum(x, y: i32) {
x + y;
}
```
2. 🌟
```rust,editable
fn main() {
print();
}
// replace i32 with another type
fn print() -> i32 {
println!("Success!")
}
```
3. 🌟🌟🌟
```rust,editable
// solve it in two ways
// DON'T let `println!` works
fn main() {
never_return();
println!("Failed!")
}
fn never_return() -> ! {
// implement this function, don't modify the fn signatures
}
```
### Diverging functions
Diverging functions never return to the caller, so they may be used in places where a value of any type is expected.
4. 🌟🌟
```rust,editable
fn main() {
println!("Success!");
}
fn get_option(tp: u8) -> Option<i32> {
match tp {
1 => {
// TODO
}
_ => {
// TODO
}
};
// Rather than returning a None, we use a diverging function instead
never_return_fn()
}
// IMPLEMENT this function in THREE ways
fn never_return_fn() -> ! {
}
```
5. 🌟🌟
```rust,editable
fn main() {
// FILL in the blank
let b = __;
let v = match b {
true => 1,
// Diverging functions can also be used in match expression to replace a value of any value
false => {
println!("Success!");
panic!("we have no value for `false`, but we can panic")
}
};
println!("Excercise Failed if printing out this line!");
}
```
> 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

View File

@ -0,0 +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

@ -0,0 +1,181 @@
# Numbers
### Integer
1. 🌟
> Tips: If we don't explicitly give one type to a varible, then the compiler will infer one for us
```rust,editable
// remove something to make it work
fn main() {
let x: i32 = 5;
let mut y: u32 = 5;
y = x;
let z = 10; // type of z ?
println!("Success!")
}
```
2. 🌟
```rust,editable
// fill the blank
fn main() {
let v: u16 = 38_u8 as __;
println!("Success!")
}
```
3. 🌟🌟🌟
> Tips: If we don't explicitly give one type to a varible, then the compiler will infer one for us
```rust,editable
// modify `assert_eq!` to make it work
fn main() {
let x = 5;
assert_eq!("u32".to_string(), type_of(&x));
println!("Success!")
}
// 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,editable
// fill the blanks to make it work
fn main() {
assert_eq!(i8::MAX, __);
assert_eq!(u8::MAX, __);
println!("Success!")
}
```
5. 🌟🌟
```rust,editable
// fix errors and panics to make it work
fn main() {
let v1 = 251_u8 + 8;
let v2 = i8::checked_add(251, 8).unwrap();
println!("{},{}",v1,v2);
}
```
6. 🌟🌟
```rust,editable
// modify `assert!` to make it work
fn main() {
let v = 1_024 + 0xff + 0o77 + 0b1111_1111;
assert!(v == 1579);
println!("Success!")
}
```
### Floating-Point
7. 🌟
```rust,editable
// replace ? with your answer
fn main() {
let x = 1_000.000_1; // ?
let y: f32 = 0.12; // f32
let z = 0.01_f64; // f64
println!("Success!")
}
```
1. 🌟🌟 make it work in two distinct ways
```rust,editable
fn main() {
assert!(0.1+0.2==0.3);
println!("Success!")
}
```
### Range
9. 🌟🌟 two goals: 1. modify `assert!` to make it work 2. make `println!` output: 97 - 122
```rust,editable
fn main() {
let mut sum = 0;
for i in -3..2 {
sum += i
}
assert!(sum == -3);
for c in 'a'..='z' {
println!("{}",c);
}
}
```
10. 🌟🌟
```rust,editable
// fill the blanks
use std::ops::{Range, RangeInclusive};
fn main() {
assert_eq!((1..__), Range{ start: 1, end: 5 });
assert_eq!((1..__), RangeInclusive::new(1, 5));
println!("Success!")
}
```
### Computations
11. 🌟
```rust,editable
// fill the blanks and fix the errors
fn main() {
// Integer addition
assert!(1u32 + 2 == __);
// Integer subtraction
assert!(1i32 - 2 == __);
assert!(1u8 - 2 == -1);
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);
}
```
> 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

View File

@ -0,0 +1,70 @@
# Statements and Expressions
### Examples
```rust,editable
fn main() {
let x = 5u32;
let y = {
let x_squared = x * x;
let x_cube = x_squared * x;
// This expression will be assigned to `y`
x_cube + x_squared + x
};
let z = {
// The semicolon suppresses this expression and `()` is assigned to `z`
2 * x;
};
println!("x is {:?}", x);
println!("y is {:?}", y);
println!("z is {:?}", z);
}
```
### Exercises
1. 🌟🌟
```rust,editable
// make it work with two ways
fn main() {
let v = {
let mut x = 1;
x += 2
};
assert_eq!(v, 3);
println!("Success!")
}
```
2. 🌟
```rust,editable
fn main() {
let v = (let x = 3);
assert!(v == 3);
println!("Success!")
}
```
3. 🌟
```rust,editable
fn main() {
let s = sum(1 , 2);
assert_eq!(s, 3);
println!("Success!")
}
fn sum(x: i32, y: i32) -> i32 {
x + y;
}
```
> 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