according to the idea of gtm-nayan #144, add println!("Success") for each exercise

This commit is contained in:
sunface
2022-03-05 14:48:43 +08:00
parent 1000a3dc6e
commit 5394cdfcff
21 changed files with 201 additions and 13 deletions

View File

@ -12,6 +12,8 @@ fn main() {
let c2 = '中';
assert_eq!(size_of_val(&c2),3);
println!("Success!")
}
```
@ -33,13 +35,13 @@ fn print_char(c : char) {
3. 🌟
```rust, editable
// make the println! work
// make println! work
fn main() {
let _f: bool = false;
let t = true;
if !t {
println!("hello, world");
println!("Success!")
}
}
```
@ -52,6 +54,8 @@ fn main() {
let f = true;
let t = true && false;
assert_eq!(t, f);
println!("Success!")
}
```
@ -66,6 +70,8 @@ fn main() {
let v = (2, 3);
assert_eq!(v, implicitly_ret_unit())
println!("Success!")
}
fn implicitly_ret_unit() {
@ -86,6 +92,8 @@ use std::mem::size_of_val;
fn main() {
let unit: () = ();
assert!(size_of_val(&unit) == 4);
println!("Success!")
}
```

View File

@ -8,6 +8,8 @@ fn main() {
let s = sum(x, y);
assert_eq!(s, 3);
println!("Success!")
}
fn sum(x, y: i32) {
@ -24,7 +26,7 @@ fn main() {
// replace i32 with another type
fn print() -> i32 {
println!("hello,world");
println!("Success!")
}
```
@ -33,8 +35,11 @@ fn print() -> i32 {
```rust,editable
// solve it in two ways
// DON'T let `println!` works
fn main() {
never_return();
println!("Failed!")
}
fn never_return() -> ! {

View File

@ -16,6 +16,8 @@ fn main() {
y = x;
let z = 10; // type of z ?
println!("Success!")
}
```
@ -25,6 +27,8 @@ fn main() {
// fill the blank
fn main() {
let v: u16 = 38_u8 as __;
println!("Success!")
}
```
@ -38,6 +42,8 @@ fn main() {
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"
@ -53,6 +59,8 @@ fn type_of<T>(_: &T) -> String {
fn main() {
assert_eq!(i8::MAX, __);
assert_eq!(u8::MAX, __);
println!("Success!")
}
```
@ -74,6 +82,8 @@ fn main() {
fn main() {
let v = 1_024 + 0xff + 0o77 + 0b1111_1111;
assert!(v == 1579);
println!("Success!")
}
```
@ -88,6 +98,8 @@ fn main() {
let x = 1_000.000_1; // ?
let y: f32 = 0.12; // f32
let z = 0.01_f64; // f64
println!("Success!")
}
```
@ -97,6 +109,8 @@ fn main() {
fn main() {
assert!(0.1+0.2==0.3);
println!("Success!")
}
```
@ -126,6 +140,8 @@ use std::ops::{Range, RangeInclusive};
fn main() {
assert_eq!((1..__), Range{ start: 1, end: 5 });
assert_eq!((1..__), RangeInclusive::new(1, 5));
println!("Success!")
}
```

View File

@ -35,6 +35,8 @@ fn main() {
};
assert_eq!(v, 3);
println!("Success!")
}
```
@ -45,6 +47,8 @@ fn main() {
let v = (let x = 3);
assert!(v == 3);
println!("Success!")
}
```
@ -54,6 +58,8 @@ fn main() {
fn main() {
let s = sum(1 , 2);
assert_eq!(s, 3);
println!("Success!")
}
fn sum(x: i32, y: i32) -> i32 {

View File

@ -19,6 +19,8 @@ fn main() {
// modify below to make it work
assert!(arr.len() == 4);
println!("Success!")
}
```
@ -34,6 +36,8 @@ fn main() {
// Arrays are stack allocated, `std::mem::size_of_val` return the bytes which array occupies
// A char takes 4 byte in Rust: Unicode char
assert!(std::mem::size_of_val(&arr) == __);
println!("Success!")
}
```
@ -47,6 +51,8 @@ fn main() {
assert!(list[0] == 1);
assert!(list.len() == 100);
println!("Success!")
}
```
@ -56,6 +62,8 @@ fn main() {
fn main() {
// fix the error
let _arr = [1, 2, '3'];
println!("Success!")
}
```
@ -68,6 +76,8 @@ fn main() {
let ele = arr[1]; // only modify this line to make the code work!
assert!(ele == 'a');
println!("Success!")
}
```
@ -83,6 +93,8 @@ fn main() {
// but indexing is not safe
let _name1 = &names[2];
println!("Success!")
}
```

View File

@ -28,6 +28,8 @@ fn main() {
// a enum variant can be converted to a integer by `as`
assert_eq!(Number::One, Number1::One);
assert_eq!(Number1::One, Number2::One);
println!("Success!")
}
```
@ -45,6 +47,8 @@ enum Message {
fn main() {
let msg1 = Message::Move{__}; // instantiating with x = 1, y = 2
let msg2 = Message::Write(__); // instantiating with "hello, world!"
println!("Success!")
}
```
@ -67,6 +71,8 @@ fn main() {
} else {
panic!("NEVER LET THIS RUN");
}
println!("Success!")
}
```

View File

@ -10,6 +10,8 @@ fn main() {
let s1: [i32] = arr[0..2];
let s2: str = "hello, world" as str;
println!("Success!")
}
```
@ -26,6 +28,8 @@ fn main() {
// modify '6' to make it work
// TIPS: slice( reference ) IS NOT an array, if it is an array, then `assert!` will passed: each of the two UTF-8 chars '中' and '国' occupies 3 bytes, 2 * 3 = 6
assert!(std::mem::size_of_val(&slice) == 6);
println!("Success!")
}
```
@ -37,6 +41,8 @@ fn main() {
// fill the blanks to make the code work
let slice: __ = __;
assert_eq!(slice, &[2, 3, 4]);
println!("Success!")
}
```
@ -52,6 +58,8 @@ fn main() {
let slice2 = &s[__];
assert_eq!(slice1, slice2);
println!("Success!")
}
```
@ -64,6 +72,8 @@ fn main() {
let slice = &s[0..2];
assert!(slice == "你");
println!("Success!")
}
```

View File

@ -10,6 +10,8 @@ The type of string literal `"hello, world"` is `&str`, e.g `let s: &str = "hello
// fix error without adding new line
fn main() {
let s: str = "hello, world";
println!("Success!")
}
```
@ -42,6 +44,8 @@ fn main() {
s.push('!');
assert_eq!(s, "hello, world!");
println!("Success!")
}
```
@ -68,7 +72,9 @@ fn main() {
// Allocate new memory and store the modified string there
let s1 = s.__("dogs", "cats");
assert_eq!(s1, "I like cats")
assert_eq!(s1, "I like cats");
println!("Success!")
}
```
@ -113,6 +119,8 @@ fn greetings(s: String) {
fn main() {
let s = "hello, world".to_string();
let s1: &str = s;
println!("Success!")
}
```
@ -222,6 +230,8 @@ fn main() {
let h1 = &s1[3..5];//modify this line to fix the error, tips: `` takes 3 bytes in UTF8 format
assert_eq!(h1, "中");
println!("Success!")
}
```

View File

@ -16,6 +16,8 @@ fn main() {
name: String::from("sunface"),
age,
};
println!("Success!")
}
```
@ -34,6 +36,8 @@ impl SomeTrait for Unit { }
fn main() {
let u = Unit;
do_something_with_unit(u);
println!("Success!")
}
// fill the blank to make the code work
@ -50,6 +54,8 @@ struct Point(i32, i32, i32);
fn main() {
let v = Point(__, __, __);
check_color(v);
println!("Success!")
}
fn check_color(p: Color) {
@ -83,6 +89,8 @@ fn main() {
// fill the lank
__ = String::from("sunfei");
println!("Success!")
}
```
@ -94,7 +102,9 @@ struct Person {
name: String,
age: u8,
}
fn main() {}
fn main() {
println!("Success!")
}
fn build_person(name: String, age: u8) -> Person {
Person {
@ -123,6 +133,8 @@ fn main() {
};
let u2 = set_email(u1);
println!("Success!")
}
fn set_email(u: User) -> User {

View File

@ -8,6 +8,8 @@ fn main() {
let _t1: (u8, (i16, u32)) = (0, (-1, 1));
// fill the blanks to make the code work
let t: (u8, __, i64, __, __) = (1u8, 2u16, 3i64, "hello", String::from(", world"));
println!("Success!")
}
```
@ -18,6 +20,8 @@ fn main() {
fn main() {
let t = ("i", "am", "sunface");
assert_eq!(t.1, "sunface");
println!("Success!")
}
```
@ -43,6 +47,8 @@ fn main() {
assert_eq!(x, 1);
assert_eq!(y, "hello");
assert_eq!(z, 6.4);
println!("Success!")
}
```
@ -57,6 +63,8 @@ fn main() {
assert_eq!(x, 3);
assert_eq!(y, 1);
assert_eq!(z, 2);
println!("Success!")
}
```
@ -69,6 +77,8 @@ fn main() {
assert_eq!(x, 5);
assert_eq!(y, 6);
println!("Success!")
}
fn sum_multiply(nums: (i32, i32)) -> (i32, i32) {

View File

@ -51,6 +51,8 @@ fn main() {
panic!("NEVER LET THIS RUN")
}
}
println!("Success!")
}
```
@ -134,6 +136,8 @@ fn main() {
}
assert_eq!(n, 66);
println!("Success!")
}
```
@ -153,6 +157,8 @@ fn main() {
}
assert_eq!(n, 66);
println!("Success!")
}
```
@ -189,6 +195,8 @@ fn main() {
}
assert_eq!(count, 5);
println!("Success!")
}
```
@ -208,6 +216,8 @@ fn main() {
};
assert_eq!(result, 20);
println!("Success!")
}
```
@ -240,7 +250,9 @@ fn main() {
}
}
assert!(count == __)
assert!(count == __);
println!("Success!")
}
```

View File

@ -88,6 +88,8 @@ fn main() {
data: [1, 2]
}
];
println!("Success!")
}
```
@ -127,6 +129,8 @@ fn main() {
check_size(["hello你好"; __]); // size of &str ?
check_size(["hello你好".to_string(); __]); // size of String?
check_size(['中'; __]); // size of char ?
println!("Success!")
}

View File

@ -28,6 +28,8 @@ fn main() {
// Implicitly specified type parameter `char` to `generic()`.
generic(__);
println!("Success!")
}
```
@ -41,6 +43,8 @@ fn main() {
assert_eq!(5, sum(2i8, 3i8));
assert_eq!(50, sum(20, 30));
assert_eq!(2.46, sum(1.23, 1.23));
println!("Success!")
}
```
@ -56,6 +60,8 @@ fn main() {
fn main() {
let integer = Point { x: 5, y: 10 };
let float = Point { x: 1.0, y: 4.0 };
println!("Success!")
}
```
@ -71,6 +77,8 @@ struct Point<T> {
fn main() {
// DON'T modify here
let p = Point{x: 5, y : "hello".to_string()};
println!("Success!")
}
```
@ -118,6 +126,8 @@ fn main() {
assert_eq!(p3.x, 5);
assert_eq!(p3.y, '中');
println!("Success!")
}
```

View File

@ -106,6 +106,8 @@ fn main() {
let t = Teacher {};
assert_eq!(t.say_hi(), "Hi, I'm your new teacher");
assert_eq!(t.say_something(), "I'm not a bad teacher");
println!("Success!")
}
```
@ -177,6 +179,8 @@ fn multipl
fn main() {
assert_eq!(6, multiply(2u8, 3u8));
assert_eq!(5.0, multiply(1.0, 5.0));
println!("Success!")
}
```
@ -217,6 +221,8 @@ fn main() {
// you need to derive some trait for FooBar to make it comparable
assert_eq!(Foo + Bar, FooBar);
assert_eq!(Foo - Bar, BarFoo);
println!("Success!")
}
```
@ -456,5 +462,7 @@ fn example2() {
fn main() {
example1();
example2();
println!("Success!")
}
```

View File

@ -132,6 +132,8 @@ fn main() {
let rect1 = Rectangle { width: 30, height: 50 };
assert_eq!(rect1.area(), 1500);
println!("Success!")
}
```
@ -176,7 +178,9 @@ impl TrafficLight {
self.color = "green".to_string()
}
}
fn main() {}
fn main() {
println!("Success!")
}
```
@ -204,6 +208,8 @@ impl TrafficLight {
fn main() {
let light = TrafficLight::new();
assert_eq!(light.get_state(), "red");
println!("Success!")
}
```
@ -228,7 +234,9 @@ impl Rectangle {
}
fn main() {}
fn main() {
println!("Success!")
}
```
### Enums

View File

@ -22,6 +22,8 @@ fn main() {
// modify this line only
assert_eq!(5, y);
println!("Success!")
}
```
@ -45,7 +47,9 @@ fn borrow_object(s: &String) {}
fn main() {
let mut s = String::from("hello, ");
push_str(s)
push_str(s);
println!("Success!")
}
fn push_str(s: &mut String) {
@ -63,6 +67,8 @@ fn main() {
let p = __;
p.push_str("world");
println!("Success!")
}
```
@ -83,6 +89,8 @@ fn main() {
// check the equality of the two address strings
assert_eq!(get_addr(r1),get_addr(r2));
println!("Success!")
}
// get memory address string
@ -104,6 +112,8 @@ fn main() {
let r2 = &mut s;
println!("{}, {}", r1, r2);
println!("Success!")
}
```
@ -116,6 +126,8 @@ fn main() {
let s = String::from("hello, ");
borrow_object(&mut s)
println!("Success!")
}
fn borrow_object(s: &mut String) {}
@ -131,6 +143,8 @@ fn main() {
borrow_object(&s);
s.push_str("world");
println!("Success!")
}
fn borrow_object(s: &String) {}

View File

@ -84,6 +84,8 @@ fn main() {
let s1 = s;
s1.push_str("world")
println!("Success!")
}
```
@ -98,6 +100,8 @@ fn main() {
*y = 4;
assert_eq!(*x, 5);
println!("Success!")
}
```

View File

@ -37,6 +37,8 @@ fn main() {
let binary = __;
assert_eq!(binary, 1);
println!("Success!")
}
```
@ -61,6 +63,8 @@ fn main() {
for msg in msgs {
show_message(msg)
}
println!("Success!")
}
fn show_message(msg: Message) {
@ -91,6 +95,8 @@ fn main() {
for ab in alphabets {
assert!(__)
}
println!("Success!")
}
```
@ -113,6 +119,8 @@ fn main() {
}
assert_eq!(count, 2);
println!("Success!")
}
```
@ -129,6 +137,8 @@ fn main() {
match o {
Some(i) => {
println!("This is a really long string and `{:?}`", i);
println!("Success!")
}
_ => {}
};
@ -148,6 +158,8 @@ fn main() {
__ {
println!("foobar holds the value: {}", i);
println!("Success!")
}
}
```

View File

@ -78,6 +78,8 @@ fn main() {
Some(x) => assert!(x >= split),
None => (),
}
println!("Success!")
}
```
@ -94,6 +96,8 @@ fn main() {
assert_eq!(last, 2048);
}
}
println!("Success!")
}
```

View File

@ -8,7 +8,9 @@
fn main() {
let x: i32; // uninitialized but using, ERROR !
let y: i32; // uninitialized but also unusing, only warning
println!("{} is equal to 5", x);
assert_eq!(x, 5);
println!("Success!")
}
```
@ -20,7 +22,8 @@ fn main() {
let __ = 1;
__ += 2;
println!("{} is equal to 3", x);
assert_eq!(x, 3);
println!("Success!")
}
```
@ -90,6 +93,8 @@ fn main() {
let y = 4;
// shadowing
let y = "I can also be bound to text!";
println!("Success!")
}
```
@ -124,6 +129,8 @@ fn main() {
assert_eq!(x, 3);
assert_eq!(y, 2);
println!("Success!")
}
```
@ -142,6 +149,8 @@ fn main() {
[.., y] = [1, 2];
// fill the blank to make the code work
assert_eq!([x,y], __);
println!("Success!")
}
```

View File

@ -11,6 +11,8 @@ fn main() {
let c2 = '中';
assert_eq!(size_of_val(&c2),3);
println!("Success!")
}
```
@ -31,13 +33,13 @@ fn print_char(c : char) {
3. 🌟
```rust, editable
// println! 工作
// make println! work
fn main() {
let _f: bool = false;
let t = true;
if !t {
println!("hello, world");
println!("Success!")
}
}
```
@ -49,6 +51,8 @@ fn main() {
let f = true;
let t = true && false;
assert_eq!(t, f);
println!("Success!")
}
```
@ -63,6 +67,8 @@ fn main() {
let v = (2, 3);
assert_eq!(v, implicitly_ret_unit())
println!("Success!")
}
fn implicitly_ret_unit() {
@ -83,6 +89,8 @@ use std::mem::size_of_val;
fn main() {
let unit: () = ();
assert!(size_of_val(&unit) == 4);
println!("Success!")
}
```