「Rust筆記」修訂間的差異

出自Tan Kian-ting的維基
跳至導覽 跳至搜尋
 
(未顯示同一使用者於中間所作的 14 次修訂)
行 2: 行 2:


=Programming Rust筆記=
=Programming Rust筆記=
==Ch1概論==
參見:[[Programming Rust筆記]]
*系統程式語言有「未定義行為」。
*使用者輸入的內容可能會導致程式漏洞。
** 編譯時消除未定義行為
** 安全,亦於使用
** 平行編譯
** 0 overhead的C++,只有必要花費,不會花費太多資源消耗
** 善用底層
** cargo、trait、generic
==Ch2 Rust特性簡單導覽==
* rust doc產生文件
* rust編譯器
* cargo編譯管理器
** cargo clean清除編譯文件
 
 
沒有<code>;</code>結尾的視為回傳值。
 
<pre>fn foo(mut n :i64) -> i64{
...
}</pre>
 
<code>assert!(a == b)</code>錯誤時拋出panic
 
 
* identifier後面的<code>!</code>為巨集
* <code>let x = m</code>可以這樣寫,會儘可能型別推論。比較<code>let x : i64 = m</code>。


=其他=
=其他=
==套件管理==
===cargo===
====加入新函式庫====
<code>cargo add csv</code>


==編輯器==
===Emacs===
* [https://github.com/rust-lang/rust-mode rust-mode]
** C-c C-c C-r:跑程式
** C-c C-f:改善排版,要先裝rustfmt
==型別==
==型別==
===函數===
===函數===
行 50: 行 33:
# 得到 UTF-8 字串長度的方法:[https://docs.rs/unicode-segmentation/1.6.0/unicode_segmentation/index.html UnicodeSegmentation::graphemes](參見:[https://stackoverflow.com/questions/46290655/get-the-string-length-in-characters-in-rust Stack Overflow])
# 得到 UTF-8 字串長度的方法:[https://docs.rs/unicode-segmentation/1.6.0/unicode_segmentation/index.html UnicodeSegmentation::graphemes](參見:[https://stackoverflow.com/questions/46290655/get-the-string-length-in-characters-in-rust Stack Overflow])
# [https://stackoverflow.com/questions/45519176/how-do-i-use-or-import-a-local-rust-file 匯入外部函式庫]
# [https://stackoverflow.com/questions/45519176/how-do-i-use-or-import-a-local-rust-file 匯入外部函式庫]
===印出當前目錄(current directory)===
<pre>
use std::env;
...
    println!("{:?}", env::current_dir());
</pre>
執行結果:
<code>Ok("[project_dir]")</code>
===sysargv 傳入程式的引數===
求出引數個數長度,沒輸入自定引數則為0:
<pre>
use std::env;
...
    println!("{}", env::args().len());
</pre>


==變數==
==變數==
行 60: 行 61:
# 獲得子字串:<code>a = a[..1].to_string();</code>
# 獲得子字串:<code>a = a[..1].to_string();</code>


== Gtk4 ==
==食譜 (Cookbook)==
===子目錄的item用for遍歷,轉成字串===
<pre>
    let corpus_CSV_paths = fs::read_dir("./corpus").unwrap();
    for path in corpus_CSV_paths {
      let path_string = format!("{:?}", path.unwrap().path());
    }
</pre>
 
===讀檔案顯示資料===
摘自:https://doc.rust-lang.org/book/ch12-02-reading-a-file.html
<pre>
let contents = fs::read_to_string(file_path)
        .expect("Should have been able to read the file");
 
    println!("With text:\n{contents}");</pre>
 
==函式庫==
=== Gtk4 ===
# [[Grid於Rust的Gtk4 binding]]
# [[Grid於Rust的Gtk4 binding]]


[[category:資訊]]
[[category:資訊]]

於 2024年7月29日 (一) 23:08 的最新修訂

Programming Rust筆記

參見:Programming Rust筆記

其他

套件管理

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