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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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.2k
Use Macro all the time ~ マクロを使いまくろ ~ 感想戦
osyo
0
350
Use Macro all the time ~ マクロを使いまくろ ~ (English)
osyo
3
490
Use Macro all the time ~ マクロを使いまくろ ~ (日本語)
osyo
0
2.4k
月単位でイテレーションする
osyo
0
390
Ruby 3.0 で変わった private と attr_xxx
osyo
1
840
Ruby 2.0 から Ruby 3.0 を駆け足で振り返る
osyo
0
2.6k
Other Decks in Programming
See All in Programming
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
400
SalesForceを内製化!? ~ HR事業を支える顧客管理基盤のインフラを大公開 ~
oku053
0
170
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
560
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
5.1k
Rでドレミ/Do-Re-Mi_with_R
florets1
0
130
個人開発者よ、Honoを使え(おじさんver)
tanahiro2010
2
250
JRuby: Past, Present, and Future
headius
0
230
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1.8k
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
1.2k
プロダクトコードからライブラリの境界を見つける
elmetal
PRO
0
110
The Rails Doctrine Decade
koic
2
560
Workers Cache を知る
syumai
0
350
Featured
See All Featured
Paper Plane
katiecoart
PRO
4
54k
Optimizing for Happiness
mojombo
378
71k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
50
10k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.8k
Heart Work Chapter 1 - Part 1
lfama
PRO
10
37k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
490
Building an army of robots
kneath
307
47k
ラッコキーワード サービス紹介資料
rakko
1
5.3M
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
590
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
1
570
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
ご清聴 ご清聴 ありがとうございました ありがとうございました