Skip to content

Commit 4740580

Browse files
committed
Added another macro sample
1 parent d9c3efe commit 4740580

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

Lesson_13/README.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,71 @@ makrolar dahil isimlendirmelerde fonksiyon adı ! işareti ile sonlandırılır.
7575
bloğunun üretimi sağlanır.
7676
- ($x:expr, $($y:expr),+) => { ... } : Bu kalıpsa iki veya daha fazla argüman için geçerlidir. İkinci söz diziminde yer
7777
alan + operaötörü en az bir veya daha fazla anlamındadır. Bu durumla karşılaşılması halinde recursive olarak kendisini
78-
çağıran bir fonksiyon kodu üretilecektir.
78+
çağıran bir fonksiyon kodu üretilecektir.
79+
80+
## MetaSyntactic Variables
81+
82+
Makrolarda ifadeleri analiz etmek ve eşleşmeleri yakalamak için token'lar kullanılır. Bunlardan en çok kullanılanlar
83+
aşağıdaki tabloda belirtilmektedir.
84+
85+
| Token | Açıklama | Örnek |
86+
|-----------|-----------------------------------------------------------------------|---------------------------------------------------|
87+
| `ident` | Değişken, fonksiyon, struct adı gibi tanımlayıcıyıları temsil edir | `User`, `my_function`, `x` |
88+
| `ty` | Belirli bir türü temsil eder _(örneğin, `f32`, `String`, `Vec<i32>`)_ | `f32`, `String`, `Option<T>` |
89+
| `expr` | Bir expression anlamına gelir. | `5 + 4`, `"hello world"`, `vec![1, 2, 3, 4, 10]` |
90+
| `stmt` | Bir ifade ya da bildirim anlamına gelir. | `let range = 50;`, `return 3.14;` |
91+
| `path` | Modül ya da tür yolu için kullanılır | `std::io::Read`, `crate::module::function` |
92+
| `literal` | Sabit değer anlamına gelir (string, sayı, boolean). | `23`, `"rustacean"`, `false` |
93+
| `block` | `{}` bloğunu temsil eder. | `{ let x = 10; x + 1 }` |
94+
| `item` | struct, enum, fn gibi enstrümanları temsil eder. | `struct Product;`, `fn send_email() {}` |
95+
| `meta` | Bir attribute' u temsil eder. | `#[derive(Debug)]`, `#[cfg(target_os = "linux")]` |
96+
| `tt` | Herhangi bir "token tree" ağacını temsil eder. | Herhangi bir Rust kodu parçası olabilir |
97+
98+
## Örnekler
99+
100+
Aşağıdaki kod parçalarında farklı senaryoların ele alındığı procedural makrolar yer almaktadır. İlk örnek bir model
101+
nesnesi için gerekli struct'ı kolayca oluşturmak için kullanılır.
102+
103+
```rust
104+
macro_rules! crud {
105+
($struct_name:ident, $($field_name:ident: $field_type:ty),*) => {
106+
#[derive(Debug)]
107+
struct $struct_name {
108+
$(
109+
$field_name: $field_type,
110+
)*
111+
}
112+
113+
impl $struct_name {
114+
fn new($($field_name: $field_type),*) -> $struct_name {
115+
$struct_name { $($field_name),* }
116+
}
117+
}
118+
};
119+
}
120+
121+
crud!(Product, id: i32,title: String,list_price:f32,category: String);
122+
123+
#[cfg(test)]
124+
mod tests {
125+
use super::*;
126+
#[test]
127+
fn test_crud_macro() {
128+
let c64 = Product::new(
129+
1,
130+
"C64 monitor 14.1inch".to_string(),
131+
999.99,
132+
"Retro Computer".to_string(),
133+
);
134+
assert_eq!(c64.id, 1);
135+
assert_eq!(c64.title, "C64 monitor 14.1inch".to_string());
136+
assert_eq!(c64.list_price, 999.99);
137+
assert_eq!(c64.category, "Retro Computer".to_string());
138+
}
139+
}
140+
```
141+
142+
Örneğin Product, Customer, Order, Category ve benzeri entity nesnelerinin yer aldığı bir senaryoda her birisi için ayrı
143+
ayrı struct geliştirmek yerine bir makro ile kod tekrarlarının önüne geçebilir, veri yapılarını basitçe
144+
tanımlayabiliriz. crud isimli makro argüman olarak gelen identifier ve type bilgilerini kullanarak struct'ın temel
145+
halini inşa eder ve aynı zamanda new metodunu otomatik olarak implemente eder.

Lesson_13/src/samples.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,42 @@ macro_rules! max_of {
1515
}
1616
}
1717

18+
#[allow(unused_macros)]
19+
macro_rules! crud {
20+
($struct_name:ident, $($field_name:ident: $field_type:ty),*) => {
21+
#[derive(Debug)]
22+
struct $struct_name {
23+
$(
24+
$field_name: $field_type,
25+
)*
26+
}
27+
28+
impl $struct_name {
29+
fn new($($field_name: $field_type),*) -> $struct_name {
30+
$struct_name { $($field_name),* }
31+
}
32+
}
33+
};
34+
}
35+
36+
crud!(Product, id: i32,title: String,list_price:f32,category: String);
37+
1838
#[cfg(test)]
1939
mod tests {
40+
use super::*;
41+
#[test]
42+
fn test_crud_macro() {
43+
let c64 = Product::new(
44+
1,
45+
"C64 monitor 14.1inch".to_string(),
46+
999.99,
47+
"Retro Computer".to_string(),
48+
);
49+
assert_eq!(c64.id, 1);
50+
assert_eq!(c64.title, "C64 monitor 14.1inch".to_string());
51+
assert_eq!(c64.list_price, 999.99);
52+
assert_eq!(c64.category, "Retro Computer".to_string());
53+
}
2054

2155
#[test]
2256
fn test_max_macro() {

0 commit comments

Comments
 (0)