Merge branch 'master' of https://github.com/sunface/rust-by-practice
This commit is contained in:
commit
b669929771
|
@ -6,10 +6,10 @@
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = 5;
|
let x = 5;
|
||||||
// fill the blank
|
// Fill the blank
|
||||||
let p = __;
|
let p = __;
|
||||||
|
|
||||||
println!("the memory address of x is {:p}", p); // one possible output: 0x16fa3ac84
|
println!("the memory address of x is {:p}", p); // One possible output: 0x16fa3ac84
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -20,23 +20,23 @@ fn main() {
|
||||||
let x = 5;
|
let x = 5;
|
||||||
let y = &x;
|
let y = &x;
|
||||||
|
|
||||||
// modify this line only
|
// Modify this line only
|
||||||
assert_eq!(5, y);
|
assert_eq!(5, y);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
3. ๐
|
3. ๐
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fix error
|
// Fix error
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut s = String::from("hello, ");
|
let mut s = String::from("hello, ");
|
||||||
|
|
||||||
borrow_object(s);
|
borrow_object(s);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn borrow_object(s: &String) {}
|
fn borrow_object(s: &String) {}
|
||||||
|
@ -45,13 +45,13 @@ fn borrow_object(s: &String) {}
|
||||||
4. ๐
|
4. ๐
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fix error
|
// Fix error
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut s = String::from("hello, ");
|
let mut s = String::from("hello, ");
|
||||||
|
|
||||||
push_str(s);
|
push_str(s);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn push_str(s: &mut String) {
|
fn push_str(s: &mut String) {
|
||||||
|
@ -65,16 +65,16 @@ fn push_str(s: &mut String) {
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut s = String::from("hello, ");
|
let mut s = String::from("hello, ");
|
||||||
|
|
||||||
// fill the blank to make it work
|
// Fill the blank to make it work
|
||||||
let p = __;
|
let p = __;
|
||||||
|
|
||||||
p.push_str("world");
|
p.push_str("world");
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### ref
|
#### Ref
|
||||||
`ref` can be used to take references to a value, similar to `&`.
|
`ref` can be used to take references to a value, similar to `&`.
|
||||||
|
|
||||||
6. ๐๐๐
|
6. ๐๐๐
|
||||||
|
@ -84,18 +84,18 @@ fn main() {
|
||||||
let c = 'ไธญ';
|
let c = 'ไธญ';
|
||||||
|
|
||||||
let r1 = &c;
|
let r1 = &c;
|
||||||
// fill the blank๏ผdont change other code
|
// Fill the blank๏ผdont change other code
|
||||||
let __ r2 = c;
|
let __ r2 = c;
|
||||||
|
|
||||||
assert_eq!(*r1, *r2);
|
assert_eq!(*r1, *r2);
|
||||||
|
|
||||||
// check the equality of the two address strings
|
// Check the equality of the two address strings
|
||||||
assert_eq!(get_addr(r1),get_addr(r2));
|
assert_eq!(get_addr(r1),get_addr(r2));
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
// get memory address string
|
// Get memory address string
|
||||||
fn get_addr(r: &char) -> String {
|
fn get_addr(r: &char) -> String {
|
||||||
format!("{:p}", r)
|
format!("{:p}", r)
|
||||||
}
|
}
|
||||||
|
@ -105,8 +105,8 @@ fn get_addr(r: &char) -> String {
|
||||||
7. ๐
|
7. ๐
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// remove something to make it work
|
// Remove something to make it work
|
||||||
// don't remove a whole line !
|
// Don't remove a whole line !
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut s = String::from("hello");
|
let mut s = String::from("hello");
|
||||||
|
|
||||||
|
@ -115,21 +115,21 @@ fn main() {
|
||||||
|
|
||||||
println!("{}, {}", r1, r2);
|
println!("{}, {}", r1, r2);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Mutablity
|
#### Mutability
|
||||||
8. ๐ Error: Borrow a immutable object as mutable
|
8. ๐ Error: Borrow an immutable object as mutable
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
//fix error by modifying this line
|
// Fix error by modifying this line
|
||||||
let s = String::from("hello, ");
|
let s = String::from("hello, ");
|
||||||
|
|
||||||
borrow_object(&mut s);
|
borrow_object(&mut s);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn borrow_object(s: &mut String) {}
|
fn borrow_object(s: &mut String) {}
|
||||||
|
@ -138,7 +138,7 @@ fn borrow_object(s: &mut String) {}
|
||||||
9. ๐๐ Ok: Borrow a mutable object as immutable
|
9. ๐๐ Ok: Borrow a mutable object as immutable
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// this code has no errors!
|
// This code has no errors!
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut s = String::from("hello, ");
|
let mut s = String::from("hello, ");
|
||||||
|
|
||||||
|
@ -146,7 +146,7 @@ fn main() {
|
||||||
|
|
||||||
s.push_str("world");
|
s.push_str("world");
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn borrow_object(s: &String) {}
|
fn borrow_object(s: &String) {}
|
||||||
|
@ -156,7 +156,7 @@ fn borrow_object(s: &String) {}
|
||||||
10. ๐๐
|
10. ๐๐
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// comment one line to make it work
|
// Comment one line to make it work
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut s = String::from("hello, ");
|
let mut s = String::from("hello, ");
|
||||||
|
|
||||||
|
@ -178,8 +178,8 @@ fn main() {
|
||||||
let r1 = &mut s;
|
let r1 = &mut s;
|
||||||
let r2 = &mut s;
|
let r2 = &mut s;
|
||||||
|
|
||||||
// add one line below to make a compiler error: cannot borrow `s` as mutable more than once at a time
|
// Add one line below to make a compiler error: cannot borrow `s` as mutable more than once at a time
|
||||||
// you can't use r1 and r2 at the same time
|
// You can't use r1 and r2 at the same time
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// use as many approaches as you can to make it work
|
// Use as many approaches as you can to make it work
|
||||||
let x = String::from("hello, world");
|
let x = String::from("hello, world");
|
||||||
let y = x;
|
let y = x;
|
||||||
println!("{},{}",x,y);
|
println!("{},{}",x,y);
|
||||||
|
@ -39,7 +39,7 @@ fn main() {
|
||||||
// Only modify the code below!
|
// Only modify the code below!
|
||||||
fn give_ownership() -> String {
|
fn give_ownership() -> String {
|
||||||
let s = String::from("hello, world");
|
let s = String::from("hello, world");
|
||||||
// convert String to Vec
|
// Convert String to Vec
|
||||||
let _s = s.into_bytes();
|
let _s = s.into_bytes();
|
||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ fn give_ownership() -> String {
|
||||||
|
|
||||||
4. ๐๐
|
4. ๐๐
|
||||||
```rust,editable
|
```rust,editable
|
||||||
// fix the error without removing code line
|
// Fix the error without removing code line
|
||||||
fn main() {
|
fn main() {
|
||||||
let s = String::from("hello, world");
|
let s = String::from("hello, world");
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ fn print_str(s: String) {
|
||||||
|
|
||||||
5. ๐๐
|
5. ๐๐
|
||||||
```rust, editable
|
```rust, editable
|
||||||
// don't use clone ,use copy instead
|
// Don't use clone ,use copy instead
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = (1, 2, (), "hello".to_string());
|
let x = (1, 2, (), "hello".to_string());
|
||||||
let y = x.clone();
|
let y = x.clone();
|
||||||
|
@ -80,12 +80,12 @@ Mutability can be changed when ownership is transferred.
|
||||||
fn main() {
|
fn main() {
|
||||||
let s = String::from("hello, ");
|
let s = String::from("hello, ");
|
||||||
|
|
||||||
// modify this line only !
|
// Modify this line only !
|
||||||
let s1 = s;
|
let s1 = s;
|
||||||
|
|
||||||
s1.push_str("world");
|
s1.push_str("world");
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -95,13 +95,13 @@ fn main() {
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = Box::new(5);
|
let x = Box::new(5);
|
||||||
|
|
||||||
let ... // implement this line, dont change other lines!
|
let ... // Implement this line, dont change other lines!
|
||||||
|
|
||||||
*y = 4;
|
*y = 4;
|
||||||
|
|
||||||
assert_eq!(*x, 5);
|
assert_eq!(*x, 5);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -148,7 +148,7 @@ fn main() {
|
||||||
|
|
||||||
let _s = t.0;
|
let _s = t.0;
|
||||||
|
|
||||||
// modify this line only, don't use `_s`
|
// Modify this line only, don't use `_s`
|
||||||
println!("{:?}", t);
|
println!("{:?}", t);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -159,7 +159,7 @@ fn main() {
|
||||||
fn main() {
|
fn main() {
|
||||||
let t = (String::from("hello"), String::from("world"));
|
let t = (String::from("hello"), String::from("world"));
|
||||||
|
|
||||||
// fill the blanks
|
// Fill the blanks
|
||||||
let (__, __) = __;
|
let (__, __) = __;
|
||||||
|
|
||||||
println!("{:?}, {:?}, {:?}", s1, s2, t); // -> "hello", "world", ("hello", "world")
|
println!("{:?}, {:?}, {:?}", s1, s2, t); // -> "hello", "world", ("hello", "world")
|
||||||
|
|
Loadingโฆ
Reference in New Issue