Skip to content

Instantly share code, notes, and snippets.

View KmolYuan's full-sized avatar
🦀
Rust programmer!

Yuan Chang KmolYuan

🦀
Rust programmer!
  • National Taiwan University (Ph.D. Student)
  • Taipei, Taiwan
  • 23:53 (UTC +08:00)
View GitHub Profile

Copy-On-Write in Rust

Rust 中常使用「移動 (move)」語意,為使複製成本降低,大部分的函式會採用 pass-by-reference 的方式 &T,但是移動語意的好處在於可以提前刪除物件。

標準庫提供的 enum 類型 {std, alloc}::borrow::Cow 同時允許「借出的 (Borrowed)」與「擁有的 (Owned)」兩種資料型態,並且會在借出可變引用 &mut T 時複製,亦可單純借出唯讀引用。

pub enum Cow<'a, B>
where
    B: 'a + ToOwned + ?Sized,
@KmolYuan
KmolYuan / rust_cffi_types.md
Last active May 10, 2024 09:48
C/Rust 類型對應表

C/Rust 類型對應表

1 byte = 8 bit,sizeof (C/C++) 和 {std, core}::mem::size_of (Rust) 回傳的是 bytes。

sizeof(double) // 8
std::mem::size_of::() // 8
@KmolYuan
KmolYuan / rust-learning-roadmap.md
Last active December 25, 2022 14:31
Rust Learning Roadmap.

Rust 閉包 Closure 解說

閉包又稱匿名函式 (Anonymous function)、Lambda 函式等,對程式語言來說是個抽象 (abstract) 的語意。與普通的靜態函式 (Static function) 相比,多出了「捕捉 (capture)」的功能,可以將自身以外的變數拿來引用。

靜態函式僅允許變數以「參數 (argument)」的方式輸入,當有大量參數必須輸入時,可以建立一個結構體 (structure) 類型來包裝相關的變數。閉包的原理亦是如此,使用一個暫時的結構體將變數存入,再將其他參數傳入,執行其專用函式。

let a = 10;
let closure = |x: i32| x * a;
closure(10);

指派 (Assignment)

指派行為是程式語言中重要的一環,除了常見的 = 運算子以外,表達式 (expression) 之間的傳遞也是指派的一種,例如呼叫函式 (functions)、運算子 (operators) 等。

res = foo(arg1, arg2) + arg3

變數 (variables) 在控制流程 (control flow) 上會經常性的重複引用,變數在實作上會跟記憶體連結,有幾種辦法:

Rust WASM 指南

JavaScript 到 Rust

JavaScript (JS) 可以將物件傳入 Rust,使用 wasm_bindgen::JsValue 類型表示。實際上就是 JS 的 reference counter 系統,與 Rust 的 Arc<RwLock<T>> 類似,但是可以自動鎖定進行寫入讀取。

但是若要細分 JS 物件,要引入 js-sys crate 提供更細節的類型轉換,有點在 C 語言裡撰寫 Python 程式的味道,因此不建議在這邊蹚渾水。Function 就是其中一例,建議直接寫好 JS 的函式,不要跟 Rust 寫在一起。註冊到瀏覽器的 window 物件後,等於是變成了 global 物件,Rust 就可以找到了。

要注意的是,若 wasm-pack 選擇 "web" 方式編譯,會變成 ECMAScript 的 module 模式,也建議用 import 語法導入,這個狀態下的 varfunction 不再是 global 變數,必須寫入 window 物件。

@KmolYuan
KmolYuan / apa-ntu.csl
Last active November 23, 2021 15:22
Citation format for NTU paper.
<?xml version="1.0" encoding="utf-8"?>
<style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" version="1.0" demote-non-dropping-particle="sort-only">
<info>
<title>American Psychological Association - NTU version</title>
<title-short>APA - NTU</title-short>
<id>https://gist.githubusercontent.com/KmolYuan/147a092e507ca0f8cc01d795f96dd4b3/raw</id>
<link href="https://gist.githubusercontent.com/KmolYuan/147a092e507ca0f8cc01d795f96dd4b3/raw" rel="self"/>
<link href="http://www.zotero.org/styles/apa" rel="template"/>
<link href="https://apastyle.apa.org/style-grammar-guidelines/references/examples" rel="documentation"/>
<author>
@KmolYuan
KmolYuan / fortran-cmake.md
Last active October 13, 2021 08:46
Configuration of compiling Fortran with CMake.

用 CMake 編譯 Fortran Project

CMake: https://cmake.org/download/

新增一個 CMakeList.txt 檔案在 project root,test-fortran 替換成 project name。 其中 add_executable function 會列出全部 source code 檔案的名稱,有新的檔案時務必新增到其中。

以下副檔名 (filename extension) 的排列為 Unix / macOS(?) / Windows。

要產生 Executable (* / *.app / *.exe) 使用 add_executable,要產生 Libray 則使用 add_library

@KmolYuan
KmolYuan / tablet.sh
Last active December 12, 2020 04:52
Huion tablet using Wacom driver in Linux Krita.
#!/bin/bash
# Wait for the GUI to be ready
while [[ ! $(pgrep plasmashell) ]]; do sleep 1; done
xsetwacom set "HID 256c:006d Pad pad" button 1 key 2
xsetwacom set "HID 256c:006d Pad pad" button 2 key Ctrl S
xsetwacom set "HID 256c:006d Pad pad" button 3 key -
xsetwacom set "HID 256c:006d Pad pad" button 8 key +
xsetwacom set "HID 256c:006d Pad pad" button 9 key Ctrl Shift Z
@KmolYuan
KmolYuan / swappable_pair.hpp
Last active October 8, 2020 05:55
A swappable pair container for STL set or map.
#include <iostream>
#include <set>
#include <map>
template<typename T>
struct SwappablePair {
T field1, field2;
inline auto operator==(const SwappablePair &rhs) const -> bool {
return (field1 == rhs.field1 && field2 == rhs.field2) ||