🦉
Programming Notes
  • My Programming Notes
  • CKA Exam Preparation
    • Certified Kubernetes Administrator
    • Setup Minikube
    • Network Design Principles
    • Role-Based Access Control (RBAC)
    • Namespace
    • Resource Quota
    • Pod
    • Deployment
    • Deployment: Rollout
    • ConfigMap
    • Service
    • Service: kubectl expose
    • Pod: Resources Management
    • Pod & Container: Quality of Service Class
    • Pod & Container: Probes
    • Limit Range
    • Scaling: Manual
    • Scaling: Horizontal Pod Autoscaler
    • Persistent Volume & Claim
    • Secret
    • Ingress: Routing
    • Ingress: TLS
    • Ingress: Rate Limit
    • Ingress: Basic Auth
    • Ingress: CRD (Custom Resource Definition)
    • Job
    • CronJob
    • Mutli-Node Cluster
  • Golang
    • Generics
    • Context
    • Goroutines and Channels in Go
    • Goroutine: Concurrency vs Parallelism
    • Goroutine: Performance & Tradeoffs
    • JSON: omitzero
  • Rust
    • Arrays & Slices
    • Closures
    • Generics & Traits
    • Iterators
    • Run Code Simultaneously
    • String vs &str
    • Tests
    • Rustlings Exercises
      • Variables
      • Functions
      • If
      • Primitive Types
      • Vectors
      • Move Semantics
      • Structs
      • Enums and Matching Pattern
      • Strings
      • Modules
      • Hashmaps
      • Options
      • Error Handling
      • Generics
      • Traits
      • Lifetimes
      • Tests
      • Iterators
      • Smart Pointers
      • Threads
      • Macros
      • Quiz 1
      • Quiz 2
      • Quiz 3
  • Software Engineering
    • CAP Theorem
    • Circuit Breaker
    • Decoupling
    • GraphQL: Query Caching
    • HMAC Signature
    • Idempotency
    • Monolith VS Microservice
    • OWASP Top 10 2021
    • PCI DSS
    • PostgreSQL: Partitioning
    • PostgreSQL: Replication
    • Protobuf & gRPC
    • Redis: Streams
    • Resource Scaling
    • Signed URL
    • SOLID
    • Stack VS Heap
    • Stateful VS Stateless
  • Site Reliability Engineering
    • Chaos Engineering
    • Distributed Tracing
    • Kubernetes (k8s)
    • SLA, SLO, and SLI Metrics
    • Site Reliability Engineer
  • Others
    • FFMPEG Cheat sheet
Powered by GitBook
On this page
  • macros1.rs
  • macros2.rs
  • macros3.rs
  • macros4.rs
  1. Rust
  2. Rustlings Exercises

Macros

PreviousThreadsNextQuiz 1

Last updated 4 months ago

  • Macros are a way of writing code that writes other code, which is known as metaprogramming.

  • Macros can take a variable number of parameters.

  • To define a macro, we use the macro_rules! construct.

  • The #[macro_export] annotation indicates that this macro should be made available whenever the crate in which the macro is defined is brought into scope.

  • We must define macros or bring them into scope before you call them in a file, as opposed to functions you can define anywhere and call anywhere.

  • References:

macros1.rs

macro_rules! my_macro {
    () => {
        println!("Check out my macro!");
    };
}

fn main() {
    // Fix the macro call by adding `!`.
    my_macro!();
}
  • This exercise is simple we just need to add ! when calling the macro.

    my_macro!();

macros2.rs

// Define macro before using it
macro_rules! my_macro {
    () => {
        println!("Check out my macro!");
    };
}

fn main() {
    my_macro!();
}
  • This exercise also easy one.

  • We just need to move the macro definition before the main function.

macros3.rs

// Fix the compiler error without taking the macro definition out of this
// module.
mod macros {
    // add export macro attribute
    #[macro_export]
    macro_rules! my_macro {
        () => {
            println!("Check out my macro!");
        };
    }
}

fn main() {
    my_macro!();
}
  • In this exercise we need to export my_macro so it can be accessible in main function.

  • We can do this by adding #[macro_export] annotation in my_macro definition.

macros4.rs

#[rustfmt::skip]
macro_rules! my_macro {
    () => {
        println!("Check out my macro!");
    }; // add `;`
    ($val:expr) => {
        println!("Look at this other macro: {}", $val);
    }; // add `;`
}

fn main() {
    my_macro!();
    my_macro!(7777);
}
  • In this exercise we only need to add ; for each macro rules statement.

Macros