{%hackmd C-_zW0vxSXSSsOkJHknc8g %}
# Rust 的奇幻旅程 <br>
## 我是誰 我在哪 我要去哪裡
[TOC]
----
Made by FanRende (030Mortal#5525)
<img src="https://i.imgur.com/lxI7kli.png" width="40%">
---
## 參考教學
- [The Rust Programming Language](https://doc.rust-lang.org/book/)
- [非官方翻譯](https://rust-lang.tw/book-tw)
---
## 所有權 (Ownership)
每個被 allocate 的記憶體都會被一個 scope 擁有
所有權可以轉移 並且只能被一個 scope 擁有
----
### 簡單的概念
```rust=
// Rust
fn main() {
if true {
let s = 50; // 被宣告所有權給 if 這個 scope
} // 因為 scope 消失 所以 s 的記憶體被釋放
println!("{}", s); // ERROR! 已經沒有這個變數了
}
```
#### 跟 C 的比較
```c=
// C
int main() {
if(1) {
int s = 50;
}
printf("%d", s); // ERROR! 嘗試使用未定義的變數
return 0;
}
```
----
### 所有權轉移
```rust=
fn main() {
let s = String::from("hello"); // 被宣告所有權給 main 這個 scope
if true {
let s2 = s; // 所有權轉移給 if 這個 scope
// 並且 s 沒有被 assign 記憶體位置了
// 因為一塊記憶體只能被一個 scope 擁有
println!("{}", s2);
} // 因為 scope 消失 所以 s2 的記憶體被釋放
println!("{}", s); // ERROR! 已經沒有這個變數了
}
```
#### 另一個例子
```rust=
fn main() {
let s = String::from("hello"); // 被宣告所有權給 main 這個 scope
change_ownership(s); // 所有權轉移給 change_ownership 這個 scope
println!("{}", s); // ERROR! 已經沒有這個變數了
}
fn change_ownership(s: String) {
println!("{}", s);
} // 因為 scope 消失 所以 s2 的記憶體被釋放
```
----
### 歸還所有權
```rust=
fn main() {
let mut s = String::from("hello"); // 被宣告所有權給 main 這個 scope
s = change_ownership(s); // 所有權轉移給 change_ownership 這個 scope
// 但馬上又還回來 所以 s 在這行的擁有權交換了兩次
println!("{}", s);
}
fn change_ownership(s: String) -> String {
println!("{}", s);
return s; // 將所有權還給 call 這個 function 的 scope
}
```
----
### 參考 (Reference)
```rust=
fn main() {
let s = String::from("hello"); // 被宣告所有權給 main 這個 scope
use_reference(&s); // 這裡的 s 是借用,所以所有權不會被移動
println!("{}", s);
}
fn use_reference(s: &String) {
println!("{}", s);
}
```
---
參考 / 延伸閱讀:
- [理解所有權](https://rust-lang.tw/book-tw/ch04-00-understanding-ownership.html)
<div class="flex-container">
<div class="flex-content">
</div>
<div class="flex-content">
</div>
</div>
<style>
.gray {
color: gray;
font-size: 0.5em;
}
.slides .rust, .slides .c {
font-size: 0.75em !important;
line-height: 1.2em !important;
}
.mermaid {
background-color: rgba(1, 1, 1, .2) !important;
}
.slides code {
background-color: #444 !important;
border-radius: 10px;
white-space : pre-wrap !important;
padding-right: 0.1em;
padding-left: 0.1em;
}
.code-wrapper code {
background-color: inherit !important;
border-radius: inherit;
}
.flex-container {
display: flex;
justify-content: center;
}
.flex-content {
flex-grow: 1;
}
</style>
<style>
/* Customize website's scrollbar like Mac OS */
::-webkit-scrollbar {
-webkit-appearance: none;
width: 7px;
}
::-webkit-scrollbar-thumb {
border-radius: 4px;
background-color: rgba(128, 128, 128, 1);
-webkit-box-shadow: 0 0 1px rgba(255, 255, 255, .5);
}
</style>
{"metaMigratedAt":"2023-06-18T00:19:53.348Z","metaMigratedFrom":"YAML","title":"Rust 的奇幻旅程 我是誰 我在哪 我要去哪裡","breaks":true,"description":"Rust 所有權","slideOptions":"{\"theme\":\"dark\",\"transition\":\"fade\",\"previewLinks\":true}","contributors":"[{\"id\":\"82f6b599-31b8-4112-9dc5-7d7b7d6a3ebb\",\"add\":3714,\"del\":84}]"}