Rust學習筆記

2021-10-12 22:53:00 字數 1013 閱讀 3191

@[toc]rust學習筆記

初步閱讀rust的**,以學習rust語言

match 採用表示式的形式,然後根據它的值來分支。分支的每個『臂』都是 val=>expression 的形式。當值匹配時,這個臂的表示式將被執行實現。之所以稱之為 match 是因為『模式匹配』的術語,而這種正是 match 實現的形式。

let x = 5;

match x

在 rust 語言中,宣告可以用 『attributes』 來注釋。它們看起來像下面這樣:

#[test]
或者是像這樣:

#![test]
兩者的區別是!,!改變了屬性所能夠適用的事物:

#[foo]

struct foo;

mod bar

參考rust reference

1.例如:

const server_name: &'static str = "miniserver";
』 符號表示的是lifetime,在rust中,lifetime宣告在<…>裡面,用 』 + any free keyword 表示。

這是由於borrowed pointers導致的。因為有borrowed pointers,當函式返回borrowed pointers時,為了保證記憶體安全,需要關注被借用的記憶體的生命週期(lifetime)。

對於』static:當borrowed pointers指向static物件時需要宣告』static lifetime。

參考rust borrowed pointers tutorial

2.例如:

where

'a: 'b,

// 其他**

pub fn get_matches(self) -> argmatches<'a>

// 其他**}

**中的a與b代表了lifetime關鍵字。

Rust學習筆記1

use rand rng use std cmp ordering use std io fn main println 輸入數字 input match input.cmp guess ordering greater println 高了 ordering less println 低了 對比c...

Rust學習筆記 2 函式

函式在rust中用來實現特定功能。其中,main函式是很多程式的入口點。在rust中用關鍵字fn來定義乙個函式。rust 中的函式和變數名使用 snake case 規範風格。在 snake case 中,所有字母都是小寫並使用下劃線分隔單詞。例如 fn this is a function 函式的...

rust學習筆記 控制流

if與其他語言類似 fn main else rust的if只能判斷布林型別,錯誤 如下 fn main 報錯 cargo run compiling branches v0.1.0 file projects branches error e0308 mismatched types src ma...