diff --git a/en/src/SUMMARY.md b/en/src/SUMMARY.md index 322d749..e2bd8f2 100644 --- a/en/src/SUMMARY.md +++ b/en/src/SUMMARY.md @@ -7,7 +7,7 @@ - [Char, Bool and Unit](basic-types/char-bool-unit.md) - [Statements and Expressions](basic-types/statements-expressions.md) - [Functions](basic-types/functions.md) -- [Ownership and Borrowing todo](ownership/intro.md) +- [Ownership and Borrowing](ownership/intro.md) - [Ownership](ownership/ownership.md) - [Reference and Borrowing](ownership/borrowing.md) - [Compound Types todo](compound-types/intro.md) diff --git a/en/src/ownership/intro.md b/en/src/ownership/intro.md index e749a67..84a7324 100644 --- a/en/src/ownership/intro.md +++ b/en/src/ownership/intro.md @@ -1 +1,5 @@ # Ownership and Borrowing +Learning resources: +- English: [Rust Book 4.1-4.4](https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html) +- 简体中文: [Rust语言圣经 - 所有权与借用(https://course.rs/basic/ownership/index.html) + diff --git a/en/src/ownership/ownership.md b/en/src/ownership/ownership.md index 77f5a52..33f9384 100644 --- a/en/src/ownership/ownership.md +++ b/en/src/ownership/ownership.md @@ -1 +1,73 @@ # Ownership + +🌟🌟 +```rust,editable + +fn main() { + // modify this line only! use as many approaches as you can + let x = String::from("hello, world"); + let y = x; + println!("{},{}",x,y); +} +``` + +🌟🌟 +```rust,editable +// Don't modify code in main! +fn main() { + let s1 = String::from("hello, world"); + let s2 = take_ownership(s1); + + println!("{}", s2); +} + +// Only modify the code below! +fn take_ownership(s: String) { + println!("{}", s); +} +``` + + +🌟🌟 +```rust,editable + +fn main() { + let s = give_ownership(); + println!("{}", s); +} + +// Only modify the code below! +fn give_ownership() -> String { + let s = String::from("hello, world"); + // convert String to Vec + let _s = s.into_bytes(); + s +} +``` + +🌟🌟 +```rust,editable +// use clone to fix it +fn main() { + let s = String::from("hello, world"); + + print_str(s); + + println!("{}", s); +} + +fn print_str(s: String) { + println!("{}",s) +} +``` + +🌟🌟 +```rust, editable +// don't use clone ,use copy instead +fn main() { + let x = (1, 2, (), "hello"); + let y = x.clone(); + println!("{:?}, {:?}", x, y); +} +``` + diff --git a/zh-CN/src/ownership/intro.md b/zh-CN/src/ownership/intro.md index e749a67..85b4feb 100644 --- a/zh-CN/src/ownership/intro.md +++ b/zh-CN/src/ownership/intro.md @@ -1 +1,5 @@ -# Ownership and Borrowing +# 所有权与借用 +学习资料 : +- English: [Rust Book 4.1-4.4](https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html) +- 简体中文: [Rust语言圣经 - 所有权与借用(https://course.rs/basic/ownership/index.html) + diff --git a/zh-CN/src/ownership/ownership.md b/zh-CN/src/ownership/ownership.md index 77f5a52..b429a6b 100644 --- a/zh-CN/src/ownership/ownership.md +++ b/zh-CN/src/ownership/ownership.md @@ -1 +1,74 @@ -# Ownership +# 所有权 + +🌟🌟 +```rust,editable + +fn main() { + // 只能修改下面这行代码! 使用尽可能多的方法来通过编译 + let x = String::from("hello, world"); + let y = x; + println!("{},{}",x,y); +} +``` + +🌟🌟 +```rust,editable +// 不要修改 main 中的代码 +fn main() { + let s1 = String::from("hello, world"); + let s2 = take_ownership(s1); + + println!("{}", s2); +} + +// 只能修改下面的代码! +fn take_ownership(s: String) { + println!("{}", s); +} +``` + + +🌟🌟 +```rust,editable + +fn main() { + let s = give_ownership(); + println!("{}", s); +} + +// 只能修改下面的代码! +fn give_ownership() -> String { + let s = String::from("hello, world"); + // convert String to Vec + // 将 String 转换成 Vec 类型 + let _s = s.into_bytes(); + s +} +``` + +🌟🌟 +```rust,editable +// 使用 clone 来让代码通过编译 +fn main() { + let s = String::from("hello, world"); + + print_str(s); + + println!("{}", s); +} + +fn print_str(s: String) { + println!("{}",s) +} +``` + +🌟🌟 +```rust, editable +// 不要使用 clone,使用 copy 的方式替代 +fn main() { + let x = (1, 2, (), "hello"); + let y = x.clone(); + println!("{:?}, {:?}", x, y); +} +``` +