「Rust筆記」修訂間的差異

出自Tan Kian-ting的維基
跳至導覽 跳至搜尋
行 16: 行 16:
* cargo編譯管理器
* cargo編譯管理器
** cargo clean清除編譯文件
** cargo clean清除編譯文件
** cargo new --vcs none #不生成.gitignore
** cargo run # 直接編譯系統執行


 
* 通常不用寫return。沒有<code>;</code>結尾的視為回傳值。
沒有<code>;</code>結尾的視為回傳值。


* 函數定義示例:
* 函數定義示例:
行 29: 行 30:


* identifier後面的<code>!</code>為巨集
* identifier後面的<code>!</code>為巨集
* <code>let x = m</code>可以這樣寫,會儘可能型別推論。比較<code>let x : i64 = m</code>
* <code>let x = m</code>可以這樣寫,會儘可能型別推論。比較<code>let x : i64 = m</code>。let 表局域變數。
* 4空白是縮排標準。
* i32 u8 f32(C float) f64(C double)
* mut 指示可變變數
* assert! 一定會被執行。debug_assert!正式執行時候不會執行此assertion。
 
<pre>
#![test] // attribution,控制編譯器、code style,或檢視條件加入程式碼。
fn test_gcd(){
assert_eq!(foo(x), rhs); #檢查左右有沒有相等的assertion
}
</pre>
 
*Vec::new() // 類似Python的List
* use std::str::FromStr; 和 use std::env:呼叫外部函式庫
* rustup doc --std # 生成標準函式庫文件
* <code>for arg in env::args().skip(1)</code> 跳過第一個argument,取值跑迴圈。


=其他=
=其他=

於 2024年7月29日 (一) 23:00 的修訂

Programming Rust筆記

Ch1概論

  • 系統程式語言有「未定義行為」。
  • 使用者輸入的內容可能會導致程式漏洞。
    • 編譯時消除未定義行為
    • 安全,亦於使用
    • 平行編譯
    • 0 overhead的C++,只有必要花費,不會花費太多資源消耗
    • 善用底層
    • cargo、trait、generic

Ch2 Rust特性簡單導覽

  • rust doc產生文件
  • rust編譯器
  • cargo編譯管理器
    • cargo clean清除編譯文件
    • cargo new --vcs none #不生成.gitignore
    • cargo run # 直接編譯系統執行
  • 通常不用寫return。沒有;結尾的視為回傳值。
  • 函數定義示例:
fn foo(mut n :i64) -> i64{
...
}

assert!(a == b)錯誤時拋出panic


  • identifier後面的!為巨集
  • let x = m可以這樣寫,會儘可能型別推論。比較let x : i64 = m。let 表局域變數。
  • 4空白是縮排標準。
  • i32 u8 f32(C float) f64(C double)
  • mut 指示可變變數
  • assert! 一定會被執行。debug_assert!正式執行時候不會執行此assertion。
#![test] // attribution,控制編譯器、code style,或檢視條件加入程式碼。
fn test_gcd(){
assert_eq!(foo(x), rhs); #檢查左右有沒有相等的assertion
}
  • Vec::new() // 類似Python的List
  • use std::str::FromStr; 和 use std::env:呼叫外部函式庫
  • rustup doc --std # 生成標準函式庫文件
  • for arg in env::args().skip(1) 跳過第一個argument,取值跑迴圈。

其他

套件管理

cargo

加入新函式庫

cargo add csv

編輯器

Emacs

  • rust-mode
    • C-c C-c C-r:跑程式
    • C-c C-f:改善排版,要先裝rustfmt

型別

函數

struct

struct Aminal {
    species: String,
    name: String,
    weight: u64,
}


資料結構

函數

  1. 得到 UTF-8 字串長度的方法:UnicodeSegmentation::graphemes(參見:Stack Overflow
  2. 匯入外部函式庫

印出當前目錄(current directory)

use std::env;
...
    println!("{:?}", env::current_dir());

執行結果: Ok("[project_dir]")

sysargv 傳入程式的引數

求出引數個數長度,沒輸入自定引數則為0:

use std::env;
...
    println!("{}", env::args().len());

變數

  1. 使用參照的方法
    foo (x : &mut i64){...}
  2. 連結字串
    let concat_str = format!("{}{}", str1, str2);

字串

  1. 獲得子字串:a = a[..1].to_string();

食譜 (Cookbook)

子目錄的item用for遍歷,轉成字串

    let corpus_CSV_paths = fs::read_dir("./corpus").unwrap();
    for path in corpus_CSV_paths {
       let path_string = format!("{:?}", path.unwrap().path());
    }

讀檔案顯示資料

摘自:https://doc.rust-lang.org/book/ch12-02-reading-a-file.html

let contents = fs::read_to_string(file_path)
        .expect("Should have been able to read the file");

    println!("With text:\n{contents}");

函式庫

Gtk4

  1. Grid於Rust的Gtk4 binding