Merge pull request #204 from Tanish-Eagle/editing-basic-types
Editing basic types
This commit is contained in:
commit
cad85a9c32
|
@ -4,7 +4,7 @@
|
||||||
1. ๐
|
1. ๐
|
||||||
```rust, editable
|
```rust, editable
|
||||||
|
|
||||||
// make it work
|
// Make it work
|
||||||
use std::mem::size_of_val;
|
use std::mem::size_of_val;
|
||||||
fn main() {
|
fn main() {
|
||||||
let c1 = 'a';
|
let c1 = 'a';
|
||||||
|
@ -13,14 +13,14 @@ fn main() {
|
||||||
let c2 = 'ไธญ';
|
let c2 = 'ไธญ';
|
||||||
assert_eq!(size_of_val(&c2),3);
|
assert_eq!(size_of_val(&c2),3);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
2. ๐
|
2. ๐
|
||||||
```rust, editable
|
```rust, editable
|
||||||
|
|
||||||
// make it work
|
// Make it work
|
||||||
fn main() {
|
fn main() {
|
||||||
let c1 = "ไธญ";
|
let c1 = "ไธญ";
|
||||||
print_char(c1);
|
print_char(c1);
|
||||||
|
@ -35,13 +35,13 @@ fn print_char(c : char) {
|
||||||
3. ๐
|
3. ๐
|
||||||
```rust, editable
|
```rust, editable
|
||||||
|
|
||||||
// make println! work
|
// Make println! work
|
||||||
fn main() {
|
fn main() {
|
||||||
let _f: bool = false;
|
let _f: bool = false;
|
||||||
|
|
||||||
let t = true;
|
let t = true;
|
||||||
if !t {
|
if !t {
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -49,13 +49,13 @@ fn main() {
|
||||||
4. ๐
|
4. ๐
|
||||||
```rust, editable
|
```rust, editable
|
||||||
|
|
||||||
// make it work
|
// Make it work
|
||||||
fn main() {
|
fn main() {
|
||||||
let f = true;
|
let f = true;
|
||||||
let t = true && false;
|
let t = true && false;
|
||||||
assert_eq!(t, f);
|
assert_eq!(t, f);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -64,36 +64,36 @@ fn main() {
|
||||||
5. ๐๐
|
5. ๐๐
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// make it work, don't modify `implicitly_ret_unit` !
|
// Make it work, don't modify `implicitly_ret_unit` !
|
||||||
fn main() {
|
fn main() {
|
||||||
let _v: () = ();
|
let _v: () = ();
|
||||||
|
|
||||||
let v = (2, 3);
|
let v = (2, 3);
|
||||||
assert_eq!(v, implicitly_ret_unit());
|
assert_eq!(v, implicitly_ret_unit());
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn implicitly_ret_unit() {
|
fn implicitly_ret_unit() {
|
||||||
println!("I will return a ()")
|
println!("I will return a ()");
|
||||||
}
|
}
|
||||||
|
|
||||||
// don't use this one
|
// Don't use this one
|
||||||
fn explicitly_ret_unit() -> () {
|
fn explicitly_ret_unit() -> () {
|
||||||
println!("I will return a ()")
|
println!("I will return a ()");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
6. ๐๐ what's the size of the unit type?
|
6. ๐๐ What's the size of the unit type?
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// modify `4` in assert to make it work
|
// Modify `4` in assert to make it work
|
||||||
use std::mem::size_of_val;
|
use std::mem::size_of_val;
|
||||||
fn main() {
|
fn main() {
|
||||||
let unit: () = ();
|
let unit: () = ();
|
||||||
assert!(size_of_val(&unit) == 4);
|
assert!(size_of_val(&unit) == 4);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -3,13 +3,13 @@
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// don't modify the following two lines!
|
// Don't modify the following two lines!
|
||||||
let (x, y) = (1, 2);
|
let (x, y) = (1, 2);
|
||||||
let s = sum(x, y);
|
let s = sum(x, y);
|
||||||
|
|
||||||
assert_eq!(s, 3);
|
assert_eq!(s, 3);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sum(x, y: i32) {
|
fn sum(x, y: i32) {
|
||||||
|
@ -24,9 +24,9 @@ fn main() {
|
||||||
print();
|
print();
|
||||||
}
|
}
|
||||||
|
|
||||||
// replace i32 with another type
|
// Replace i32 with another type
|
||||||
fn print() -> i32 {
|
fn print() -> i32 {
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -34,16 +34,16 @@ fn print() -> i32 {
|
||||||
3. ๐๐๐
|
3. ๐๐๐
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
// solve it in two ways
|
// Solve it in two ways
|
||||||
// DON'T let `println!` works
|
// DON'T let `println!` works
|
||||||
fn main() {
|
fn main() {
|
||||||
never_return();
|
never_return();
|
||||||
|
|
||||||
println!("Failed!")
|
println!("Failed!");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn never_return() -> ! {
|
fn never_return() -> ! {
|
||||||
// implement this function, don't modify the fn signatures
|
// Implement this function, don't modify the fn signatures
|
||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -90,11 +90,11 @@ fn main() {
|
||||||
// Diverging functions can also be used in match expression to replace a value of any value
|
// Diverging functions can also be used in match expression to replace a value of any value
|
||||||
false => {
|
false => {
|
||||||
println!("Success!");
|
println!("Success!");
|
||||||
panic!("we have no value for `false`, but we can panic")
|
panic!("we have no value for `false`, but we can panic");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("Excercise Failed if printing out this line!");
|
println!("Exercise Failed if printing out this line!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -4,49 +4,49 @@
|
||||||
|
|
||||||
1. ๐
|
1. ๐
|
||||||
|
|
||||||
> Tips: If we don't explicitly give one type to a varible, then the compiler will infer one for us
|
> Tips: If we don't explicitly assign a type to a variable, then the compiler will infer one for us.
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// remove something to make it work
|
// Remove something to make it work
|
||||||
fn main() {
|
fn main() {
|
||||||
let x: i32 = 5;
|
let x: i32 = 5;
|
||||||
let mut y: u32 = 5;
|
let mut y: u32 = 5;
|
||||||
|
|
||||||
y = x;
|
y = x;
|
||||||
|
|
||||||
let z = 10; // type of z ?
|
let z = 10; // Type of z ?
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
2. ๐
|
2. ๐
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fill the blank
|
// Fill the blank
|
||||||
fn main() {
|
fn main() {
|
||||||
let v: u16 = 38_u8 as __;
|
let v: u16 = 38_u8 as __;
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
3. ๐๐๐
|
3. ๐๐๐
|
||||||
|
|
||||||
> Tips: If we don't explicitly give one type to a varible, then the compiler will infer one for us
|
> Tips: If we don't explicitly assign a type to a variable, then the compiler will infer one for us.
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// modify `assert_eq!` to make it work
|
// Modify `assert_eq!` to make it work
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = 5;
|
let x = 5;
|
||||||
assert_eq!("u32".to_string(), type_of(&x));
|
assert_eq!("u32".to_string(), type_of(&x));
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the type of given variable, return a string representation of the type , e.g "i8", "u8", "i32", "u32"
|
// 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 {
|
fn type_of<T>(_: &T) -> String {
|
||||||
format!("{}", std::any::type_name::<T>())
|
format!("{}", std::any::type_name::<T>())
|
||||||
}
|
}
|
||||||
|
@ -55,19 +55,19 @@ fn type_of<T>(_: &T) -> String {
|
||||||
4. ๐๐
|
4. ๐๐
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fill the blanks to make it work
|
// Fill the blanks to make it work
|
||||||
fn main() {
|
fn main() {
|
||||||
assert_eq!(i8::MAX, __);
|
assert_eq!(i8::MAX, __);
|
||||||
assert_eq!(u8::MAX, __);
|
assert_eq!(u8::MAX, __);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
5. ๐๐
|
5. ๐๐
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fix errors and panics to make it work
|
// Fix errors and panics to make it work
|
||||||
fn main() {
|
fn main() {
|
||||||
let v1 = 251_u8 + 8;
|
let v1 = 251_u8 + 8;
|
||||||
let v2 = i8::checked_add(251, 8).unwrap();
|
let v2 = i8::checked_add(251, 8).unwrap();
|
||||||
|
@ -78,12 +78,12 @@ fn main() {
|
||||||
6. ๐๐
|
6. ๐๐
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// modify `assert!` to make it work
|
// Modify `assert!` to make it work
|
||||||
fn main() {
|
fn main() {
|
||||||
let v = 1_024 + 0xff + 0o77 + 0b1111_1111;
|
let v = 1_024 + 0xff + 0o77 + 0b1111_1111;
|
||||||
assert!(v == 1579);
|
assert!(v == 1579);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -93,29 +93,29 @@ fn main() {
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// replace ? with your answer
|
// Replace ? with your answer
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = 1_000.000_1; // ?
|
let x = 1_000.000_1; // ?
|
||||||
let y: f32 = 0.12; // f32
|
let y: f32 = 0.12; // f32
|
||||||
let z = 0.01_f64; // f64
|
let z = 0.01_f64; // f64
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
1. ๐๐ make it work in two distinct ways
|
1. ๐๐ Make it work in two distinct ways
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
assert!(0.1+0.2==0.3);
|
assert!(0.1+0.2==0.3);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Range
|
### Range
|
||||||
9. ๐๐ two goals: 1. modify `assert!` to make it work 2. make `println!` output: 97 - 122
|
9. ๐๐ Two goals: 1. Modify `assert!` to make it work 2. Make `println!` output: 97 - 122
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -135,13 +135,13 @@ fn main() {
|
||||||
10. ๐๐
|
10. ๐๐
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fill the blanks
|
// Fill the blanks
|
||||||
use std::ops::{Range, RangeInclusive};
|
use std::ops::{Range, RangeInclusive};
|
||||||
fn main() {
|
fn main() {
|
||||||
assert_eq!((1..__), Range{ start: 1, end: 5 });
|
assert_eq!((1..__), Range{ start: 1, end: 5 });
|
||||||
assert_eq!((1..__), RangeInclusive::new(1, 5));
|
assert_eq!((1..__), RangeInclusive::new(1, 5));
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -150,7 +150,7 @@ fn main() {
|
||||||
11. ๐
|
11. ๐
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fill the blanks and fix the errors
|
// Fill the blanks and fix the errors
|
||||||
fn main() {
|
fn main() {
|
||||||
// Integer addition
|
// Integer addition
|
||||||
assert!(1u32 + 2 == __);
|
assert!(1u32 + 2 == __);
|
||||||
|
|
|
@ -27,7 +27,7 @@ fn main() {
|
||||||
### Exercises
|
### Exercises
|
||||||
1. ๐๐
|
1. ๐๐
|
||||||
```rust,editable
|
```rust,editable
|
||||||
// make it work with two ways
|
// Make it work with two ways
|
||||||
fn main() {
|
fn main() {
|
||||||
let v = {
|
let v = {
|
||||||
let mut x = 1;
|
let mut x = 1;
|
||||||
|
@ -36,7 +36,7 @@ fn main() {
|
||||||
|
|
||||||
assert_eq!(v, 3);
|
assert_eq!(v, 3);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ fn main() {
|
||||||
|
|
||||||
assert!(v == 3);
|
assert!(v == 3);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -59,7 +59,7 @@ fn main() {
|
||||||
let s = sum(1 , 2);
|
let s = sum(1 , 2);
|
||||||
assert_eq!(s, 3);
|
assert_eq!(s, 3);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sum(x: i32, y: i32) -> i32 {
|
fn sum(x: i32, y: i32) -> i32 {
|
||||||
|
|
Loadingโฆ
Reference in New Issue