「Rust筆記」修訂間的差異
跳至導覽
跳至搜尋
Tankianting(討論 | 貢獻) |
Tankianting(討論 | 貢獻) |
||
(未顯示同一使用者於中間所作的 24 次修訂) | |||
行 1: | 行 1: | ||
{{Nav|程式語言、邏輯學}} | |||
=Programming Rust筆記= | |||
參見:[[Programming Rust筆記]] | |||
=其他= | |||
==套件管理== | |||
===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 | |||
==型別== | ==型別== | ||
===函數=== | ===函數=== | ||
行 17: | 行 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> | |||
==變數== | ==變數== | ||
行 24: | 行 58: | ||
#: <code>let concat_str = format!("{}{}", str1, str2);</code> | #: <code>let concat_str = format!("{}{}", str1, str2);</code> | ||
==字串== | |||
# 獲得子字串:<code>a = a[..1].to_string();</code> | |||
==食譜 (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]] | |||
[[category:資訊]] | [[category:資訊]] |
於 2024年7月29日 (一) 23:08 的最新修訂
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, }
資料結構
- 樹的使用函式庫 Apache License 2.0/MIT
函數
- 得到 UTF-8 字串長度的方法:UnicodeSegmentation::graphemes(參見:Stack Overflow)
- 匯入外部函式庫
印出當前目錄(current directory)
use std::env; ... println!("{:?}", env::current_dir());
執行結果:
Ok("[project_dir]")
sysargv 傳入程式的引數
求出引數個數長度,沒輸入自定引數則為0:
use std::env; ... println!("{}", env::args().len());
變數
字串
- 獲得子字串:
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}");