mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 20:49:41 +00:00
Fixed missing semicolon and capitalization mistakes in the ownership
This commit is contained in:
@ -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")
|
||||||
|
Reference in New Issue
Block a user