Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Rust のマクロについて調べてみた
Search
osyo
October 27, 2020
Programming
390
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rust のマクロについて調べてみた
osyo
October 27, 2020
More Decks by osyo
See All by osyo
5分で話せる Ruby 3.1
osyo
0
210
AST を使って ActiveRecord の where の条件式をブロックで記述しよう
osyo
2
1.3k
Vim の開発環境自慢
osyo
5
3.1k
Use Macro all the time ~ マクロを使いまくろ ~ 感想戦
osyo
0
350
Use Macro all the time ~ マクロを使いまくろ ~ (English)
osyo
3
480
Use Macro all the time ~ マクロを使いまくろ ~ (日本語)
osyo
0
2.4k
月単位でイテレーションする
osyo
0
380
Ruby 3.0 で変わった private と attr_xxx
osyo
1
830
Ruby 2.0 から Ruby 3.0 を駆け足で振り返る
osyo
0
2.6k
Other Decks in Programming
See All in Programming
AgentCore CLI で進化した AWS での AI エージェントの作り方 : 必要な機能を必要な時に
icoxfog417
PRO
3
360
海上で動くGoサーバー: goroutineとchannelでさばく航行データストリーム
atsuki_seo
0
850
TiDB Cloudのカスタムコントローラーによるオートスケール対応
takaidohigasi
0
130
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
320
Java 27新機能 / Java 27 new features
kishida
2
150
スマートフォンでモールス信号を送受信する 〜スマートフォンのLEDとカメラで作る光通信の設計と実装〜
atsuki_seo
0
160
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
250
Webの地図
yosuke_furukawa
PRO
6
4.7k
手動確認はもう限界 〜XCUITestでCustom URL Schemeの遷移を起動種別ごとに自動テストする〜 / Testing Custom URL Schemes with XCUITest
otouto
0
320
巨大モノリシックアプリ モダン化大作戦
ktcryomm
1
1.1k
thread_parallel_with_free-threaded_Python_and_NumPy.pdf
riku_sakamoto
0
350
アクセシビリティから考える情報設計
high_g_engineer
0
380
Featured
See All Featured
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
57k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
570
ラッコキーワード サービス紹介資料
rakko
1
5M
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.6k
The Cult of Friendly URLs
andyhume
79
7k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
910
How to Ace a Technical Interview
jacobian
281
24k
Building the Perfect Custom Keyboard
takai
2
880
Building an army of robots
kneath
307
46k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Fireside Chat
paigeccino
43
4k
Transcript
Rust LT 会@ オンライン Rust LT 会@ オンライン Rust のマクロについて調べてみた
Rust のマクロについて調べてみた
⾃⼰紹介 ⾃⼰紹介 名前:osyo Twitter : github : ブログ : 元
C++er の現 Rails エンジニア 趣味で Ruby にパッチを投げたりしてる Rust 歴は1 ⽇ 昔 struct + trait でコンパイル時処理で遊んだことはある コンパイル時に階乗を⾏おうとしたりとか… @pink_bangbi osyo-manga Secret Garden(Instrumental) https://osyo-manga.github.io/slide-shinjukurb-58-rust/#/
Rust のマクロについて調べてみた Rust のマクロについて調べてみた
マクロとは マクロとは ソースコード中に繰り返し登場する特定の記述を、別の(短い)記 述に置き換えることができる機能をマクロという。 C / C++ ではプリプロセッサでプリプロセス時に機械的にコードを置 換する機能 Rust
でも同等の機能があるが C / C++ よりもより強⼒な機能になって いる 単純な置換ではなくて構⽂⾃体を拡張できる 構⽂⽊レベルでの拡張ができる(らしい #define PI 3.14 #define PLUS(a, b) a + b auto f = PI + PI; // auto f = 3.14 + 3.14 auto n = PLUS(1, 2) * 3 // auto n = 1 + 2 * 3
簡単な使い⽅ 簡単な使い⽅ macro_rules! 名前 {} でマクロを定義する事ができる ( 引数) => {
展開後のコード } でマクロ本体を記述する // マクロを定義する // hello がマクロ名になる macro_rules! hello { () => { println!("Hello!"); }; } fn main() { // 定義したマクロを呼び出す // hello!() のように ! を付けて関数っぽい呼び出しを⾏う hello!(); // println!("Hello!"); に置き換わる // () ではなくて [] や {} で呼び出すことができる hello![]; // {} の場合は ; がなくてもいいらしい hello!{} }
パターンを定義する パターンを定義する ( パターン) => { ... } で呼び出し元から渡された値を元に処理を分岐す ることができる
macro_rules! animal { // 引数の値で処理を切り分ける (cat) => { " にゃーん" }; (dog) => { " わーん" }; // 再帰的にマクロを呼び出すこともできる (cat and dog) => { animal!(cat).to_string() + " and " + animal! (dog) }; (cat -> dog) => { animal!(cat).to_string() + " to " + animal!(dog) }; } fn main() { // 引数の⽂字列によって呼び出されるマクロの処理が切り替わる println!("{}", animal!(cat)); // output: にゃーん println!("{}", animal!(dog)); // output: わーん println!("{}", animal!(cat and dog)); // output: にゃーん and わーん println!("{}", animal!(cat -> dog)); // output: にゃーん to わーん }
パターンを変数で受け取る パターンを変数で受け取る メタ変数と呼ばれるもので { 変数名: フラグメント指定⼦} で値を受け 取ることができる macro_rules! plus
{ // $a と $b で引数の値を受け取る // expr は『式を受け取る』という意味 // expr はフラグメント指定⼦と呼ばれる ($a: expr, $b: expr) => { $a + $b }; } macro_rules! calc { // 演算⼦も受け取る事ができる ($a: expr, $op: tt, $b: expr) => { $a $op $b }; } fn main() { println!("{}", plus!(1, 2)); // output: 3 println!("{}", plus!(1, 2 * 3)); // output: 7 println!("{}", calc!(1, -, 2)); // output: -1 }
デバッグ⽤のマクロをつくってみる デバッグ⽤のマクロをつくってみる 式とその結果の両⽅を出⼒するマクロを定義する // 式を受け取ってその式の⽂字列と結果を表⽰する macro_rules! debug { // stringify!
で受け取った値を⽂字列に変換できる ($expr: expr) => { println!("{} => {}", stringify!($expr), $expr) } } fn main() { let a = 42; debug!(1 + 2); // output: 1 + 2 => 3 debug!(a + 3); // output: a + 3 => 45 debug!(a.to_string() + "homu"); // a.to_string() + "homu" => 42homu }
マクロのスコープ マクロのスコープ マクロ内のスコープは独⽴しているので変数は外から参照できない macro_rules! test { // マクロ内のスコープは独⽴していて変数は外から参照できない () =>
{ let a = 42; println!("{}", a); }; } fn main() { test!(); // error[E0425]: cannot find value `a` in this scope a; }
マクロの優先順位( 評価順) マクロの優先順位( 評価順) マクロが先に評価される #define test 1 + 2
// 1 + 2 * 3 と展開される test * 3 // 7 macro_rules! test { () => { 1 + 2 }; } fn main() { // (1 + 2) * 3 が評価される println!("{}", test!() * 3); // output: 9 }
まとめ まとめ C / C++ と⽐べて Rust のマクロはかなり強⼒に⾒える この強⼒なマクロを Ruby
で実装したい… まだまだおもしろい使い⽅があると思うので今後も調べていきたい このあたりの話を普段から Rust を使っている⼈からいろいろと 聞いてみたい 実際のユースケースなどなど そもそも Rust のマクロの仕組みが(コンパイラや構⽂レベルで)わ かってないのでそのあたりを今後は理解していきたい
資料 資料 Rust の全マクロ種別が分かったつもりになれる話! / rust-all-kinds-of- macro ためしておぼえる Rust のマクロ
- Qiita Rust のマクロを覚える - Qiita マクロクラブ Rust ⽀部 | κeen のHappy Hacκing Blog
ご清聴 ご清聴 ありがとうございました ありがとうございました