mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 04:29:41 +00:00
Update ownership.md
This commit is contained in:
@ -5,9 +5,9 @@
|
|||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -15,7 +15,7 @@ fn main() {
|
|||||||
```rust,editable
|
```rust,editable
|
||||||
// Don't modify code in main!
|
// Don't modify code in main!
|
||||||
fn main() {
|
fn main() {
|
||||||
let s1 = String::from("hello, world");
|
let s1 = String::from("Hello world");
|
||||||
let s2 = take_ownership(s1);
|
let s2 = take_ownership(s1);
|
||||||
|
|
||||||
println!("{}", s2);
|
println!("{}", s2);
|
||||||
@ -38,7 +38,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,9 +47,9 @@ fn give_ownership() -> String {
|
|||||||
|
|
||||||
4. 🌟🌟
|
4. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
// Fix the error without removing code line
|
// Fix the error without removing any code
|
||||||
fn main() {
|
fn main() {
|
||||||
let s = String::from("hello, world");
|
let s = String::from("Hello World");
|
||||||
|
|
||||||
print_str(s);
|
print_str(s);
|
||||||
|
|
||||||
@ -77,13 +77,13 @@ Mutability can be changed when ownership is transferred.
|
|||||||
6. 🌟
|
6. 🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
|
// make the necessary variable mutable
|
||||||
fn main() {
|
fn main() {
|
||||||
let s = String::from("hello, ");
|
let s = String::from("Hello ");
|
||||||
|
|
||||||
// Modify this line only !
|
|
||||||
let s1 = s;
|
let s1 = s;
|
||||||
|
|
||||||
s1.push_str("world");
|
s1.push_str("World!");
|
||||||
|
|
||||||
println!("Success!");
|
println!("Success!");
|
||||||
}
|
}
|
||||||
@ -95,7 +95,7 @@ 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 ... // update this line, don't change other lines!
|
||||||
|
|
||||||
*y = 4;
|
*y = 4;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user