Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Quiz: บทที่ 18 - Macros

คำถาม 18.1

macro_rules! ใช้สร้าง macro แบบไหน?

A. Procedural
B. Declarative
C. Attribute
D. Derive

ดูเฉลย

B. Declarative

macro_rules! say_hello {
    () => {
        println!("Hello!");
    };
}

คำถาม 18.2

$x:expr หมายถึง?

A. Expression ที่ชื่อ x
B. Macro parameter ที่ match expression
C. Variable x
D. Type x

ดูเฉลย

B. Macro parameter ที่ match expression

Fragment specifiers:

  • expr = expression
  • ident = identifier
  • ty = type
  • stmt = statement

คำถาม 18.3

$(...)* หมายถึงอะไร?

A. Pointer
B. Zero or more repetitions
C. Multiplication
D. Reference

ดูเฉลย

B. Zero or more repetitions

macro_rules! vec_like {
    ($($x:expr),*) => { ... };
}

คำถาม 18.4

Procedural macros ต่างจาก declarative อย่างไร?

A. ไม่ต่าง
B. Procedural ทำงานกับ token stream
C. Declarative เร็วกว่า
D. Procedural อยู่ใน file เดียว

ดูเฉลย

B. Procedural ทำงานกับ token stream

Procedural macros:

  • รับ TokenStream
  • Return TokenStream
  • ต้องอยู่ใน crate แยก

คำถาม 18.5

#[derive(Debug)] คือ macro ประเภทไหน?

A. Declarative
B. Function-like
C. Derive macro
D. Attribute macro

ดูเฉลย

C. Derive macro

Derive macros auto-implement traits


👉 Quiz บทที่ 19