惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
宝玉的分享
宝玉的分享
Latest news
Latest news
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
美团技术团队
V
Visual Studio Blog
F
Full Disclosure
腾讯CDC
H
Help Net Security
D
DataBreaches.Net
M
MIT News - Artificial intelligence
罗磊的独立博客
博客园 - 司徒正美
N
Netflix TechBlog - Medium
U
Unit 42
Vercel News
Vercel News
I
InfoQ
S
SegmentFault 最新的问题
B
Blog RSS Feed
博客园 - 三生石上(FineUI控件)
Microsoft Security Blog
Microsoft Security Blog
有赞技术团队
有赞技术团队
博客园_首页
The GitHub Blog
The GitHub Blog
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
NISL@THU
NISL@THU
P
Privacy & Cybersecurity Law Blog
Recorded Future
Recorded Future
Y
Y Combinator Blog
S
Schneier on Security
P
Proofpoint News Feed
T
Tenable Blog
Cloudbric
Cloudbric
博客园 - 【当耐特】
The Register - Security
The Register - Security
人人都是产品经理
人人都是产品经理
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security
量子位
A
Arctic Wolf
N
News and Events Feed by Topic
H
Hacker News: Front Page
MongoDB | Blog
MongoDB | Blog
The Hacker News
The Hacker News
Webroot Blog
Webroot Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Rust Concepts: Macros, Modules, Testing & Unsafe Rust (Part 3)
mihir mohapa · 2026-05-20 · via DEV Community

This is Part 3 of the Core Rust Concepts series.

  • Part 1 — Ownership, Borrowing, Lifetimes, Traits, Result/Option, Pattern Matching
  • Part 2 — Closures, Iterators, Generics, Enums, Smart Pointers, Async/Await

Table of Contents

  1. Macros
  2. Modules & the Cargo Ecosystem
  3. Testing in Rust
  4. Closures as Return Types & Higher-Order Functions
  5. Unsafe Rust
  6. FFI — Calling C from Rust

13. Macros

Rust has two kinds of macros: declarative (macro_rules!) and procedural (proc macros). Both generate code at compile time — not at runtime.

Declarative Macros (macro_rules!)

These work by pattern matching on the syntax you pass in and expanding to new code.

// Define a macro
macro_rules! say_hello {
    () => {
        println!("Hello!");
    };
    ($name:expr) => {
        println!("Hello, {}!", $name);
    };
}

fn main() {
    say_hello!();            // Hello!
    say_hello!("Rustacean"); // Hello, Rustacean!
}

Enter fullscreen mode Exit fullscreen mode

A more practical example — a map! macro that builds a HashMap:

macro_rules! map {
    ($($key:expr => $val:expr),*) => {{
        let mut m = std::collections::HashMap::new();
        $(m.insert($key, $val);)*
        m
    }};
}

fn main() {
    let scores = map!("Alice" => 95, "Bob" => 87, "Carol" => 92);
    println!("{:?}", scores);
}

Enter fullscreen mode Exit fullscreen mode

Procedural Macros (Derive)

Proc macros operate on the token stream — used for #[derive(...)], custom attributes, and function-like macros.

// The most common proc macro you'll use — derive
#[derive(Debug, Clone, PartialEq)]
struct User {
    name: String,
    age: u32,
}

fn main() {
    let u1 = User { name: String::from("Alice"), age: 30 };
    let u2 = u1.clone();
    println!("{:?}", u1);       // Debug print
    println!("{}", u1 == u2);   // PartialEq: true
}

Enter fullscreen mode Exit fullscreen mode

💡 Popular crates like serde use proc macros to generate serialization/deserialization code automatically with just #[derive(Serialize, Deserialize)].


14. Modules & the Cargo Ecosystem

Modules

Rust organizes code into a module tree. You define modules with mod, control visibility with pub, and bring items into scope with use.

mod math {
    pub fn add(a: i32, b: i32) -> i32 { a + b }
    pub fn subtract(a: i32, b: i32) -> i32 { a - b }

    pub mod advanced {
        pub fn power(base: i32, exp: u32) -> i32 {
            base.pow(exp)
        }
    }
}

use math::advanced::power;

fn main() {
    println!("{}", math::add(3, 4));      // 7
    println!("{}", math::subtract(10, 4)); // 6
    println!("{}", power(2, 8));           // 256
}

Enter fullscreen mode Exit fullscreen mode

Splitting across files

src/
├── main.rs
├── math.rs          ← mod math; in main.rs
└── math/
    └── advanced.rs  ← pub mod advanced; in math.rs

Enter fullscreen mode Exit fullscreen mode

// main.rs
mod math;
use math::add;

fn main() {
    println!("{}", add(2, 3));
}

Enter fullscreen mode Exit fullscreen mode

// math.rs
pub mod advanced;

pub fn add(a: i32, b: i32) -> i32 { a + b }

Enter fullscreen mode Exit fullscreen mode

Cargo — The Build System & Package Manager

# Cargo.toml
[package]
name    = "my_app"
version = "0.1.0"
edition = "2021"

[dependencies]
serde       = { version = "1", features = ["derive"] }
tokio       = { version = "1", features = ["full"] }
reqwest     = { version = "0.11", features = ["json"] }
anyhow      = "1"

[dev-dependencies]
pretty_assertions = "1"

Enter fullscreen mode Exit fullscreen mode

Essential Cargo commands:

cargo new my_app        # create a new project
cargo build             # compile (debug)
cargo build --release   # compile (optimized)
cargo run               # build + run
cargo test              # run all tests
cargo doc --open        # generate and open docs
cargo add serde         # add a dependency
cargo update            # update dependencies
cargo clippy            # lint your code
cargo fmt               # auto-format

Enter fullscreen mode Exit fullscreen mode


15. Testing in Rust

Testing is built into the language — no extra framework needed. Tests live right next to your code.

Unit Tests

pub fn divide(a: f64, b: f64) -> Option<f64> {
    if b == 0.0 { None } else { Some(a / b) }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_divide_normal() {
        assert_eq!(divide(10.0, 2.0), Some(5.0));
    }

    #[test]
    fn test_divide_by_zero() {
        assert_eq!(divide(5.0, 0.0), None);
    }

    #[test]
    #[should_panic(expected = "index out of bounds")]
    fn test_panic() {
        let v = vec![1, 2, 3];
        let _ = v[10]; // should panic
    }

    #[test]
    fn test_with_message() {
        let result = divide(9.0, 3.0);
        assert!(result.is_some(), "expected Some, got None");
        assert!((result.unwrap() - 3.0).abs() < f64::EPSILON);
    }
}

Enter fullscreen mode Exit fullscreen mode

Run with:

cargo test                        # run all tests
cargo test test_divide            # run tests matching the name
cargo test -- --nocapture         # show println! output

Enter fullscreen mode Exit fullscreen mode

Integration Tests

Integration tests live in a top-level tests/ folder and test your public API.

src/
└── lib.rs
tests/
└── integration_test.rs

Enter fullscreen mode Exit fullscreen mode

// tests/integration_test.rs
use my_app::divide;

#[test]
fn test_public_api() {
    assert_eq!(divide(20.0, 4.0), Some(5.0));
}

Enter fullscreen mode Exit fullscreen mode

Test with Result

#[test]
fn test_returns_result() -> Result<(), String> {
    let val = divide(10.0, 2.0).ok_or("division failed")?;
    assert_eq!(val, 5.0);
    Ok(())
}

Enter fullscreen mode Exit fullscreen mode

💡 Use cargo test -- --test-threads=1 if tests share state and need to run sequentially.


16. Closures as Return Types & Higher-Order Functions

Rust can return closures from functions, enabling functional patterns like currying and function factories.

// Return a closure using impl Fn
fn make_adder(x: i32) -> impl Fn(i32) -> i32 {
    move |y| x + y
}

// Return a closure on the heap using Box<dyn Fn>
fn make_multiplier(x: i32) -> Box<dyn Fn(i32) -> i32> {
    Box::new(move |y| x * y)
}

fn apply_twice<F: Fn(i32) -> i32>(f: F, val: i32) -> i32 {
    f(f(val))
}

fn main() {
    let add5  = make_adder(5);
    let triple = make_multiplier(3);

    println!("{}", add5(10));            // 15
    println!("{}", triple(7));           // 21
    println!("{}", apply_twice(add5, 0)); // 10
    println!("{}", apply_twice(|x| x * 2, 3)); // 12
}

Enter fullscreen mode Exit fullscreen mode

When to use impl Fn vs Box<dyn Fn>:

impl Fn Box<dyn Fn>
Storage in struct
Return from function
Runtime cost Zero Small heap alloc
Works in collections

17. Unsafe Rust

Rust's safety guarantees are enforced at compile time — but sometimes you need to step outside them. unsafe blocks let you do four things the borrow checker won't normally allow.

What unsafe unlocks:

  • Dereference raw pointers
  • Call unsafe functions or C functions
  • Access or mutate static mutable variables
  • Implement unsafe traits
fn main() {
    // Raw pointers
    let x = 42;
    let r: *const i32 = &x;           // create raw pointer (safe)

    unsafe {
        println!("{}", *r);            // dereference (unsafe)
    }

    // Mutable raw pointer
    let mut y = 10;
    let rw: *mut i32 = &mut y;
    unsafe {
        *rw += 1;
    }
    println!("{}", y); // 11
}

Enter fullscreen mode Exit fullscreen mode

Unsafe functions:

unsafe fn dangerous_operation(ptr: *mut i32, len: usize) {
    for i in 0..len {
        *ptr.add(i) *= 2;
    }
}

fn main() {
    let mut data = vec![1, 2, 3, 4, 5];
    let ptr = data.as_mut_ptr();
    let len = data.len();

    unsafe {
        dangerous_operation(ptr, len);
    }

    println!("{:?}", data); // [2, 4, 6, 8, 10]
}

Enter fullscreen mode Exit fullscreen mode

Static mutable variables:

static mut COUNTER: u32 = 0;

fn increment() {
    unsafe { COUNTER += 1; }
}

fn main() {
    increment();
    increment();
    unsafe { println!("Counter: {}", COUNTER); } // 2
}

Enter fullscreen mode Exit fullscreen mode

⚠️ unsafe doesn't turn off the borrow checker entirely — it only unlocks those four specific superpowers. You're taking responsibility for ensuring correctness in those blocks. Keep unsafe blocks as small as possible and document why they're safe.


18. FFI — Calling C from Rust

Rust can interoperate with C libraries using its Foreign Function Interface. This is how Rust wraps system APIs, game engines, and existing C codebases.

Calling a C standard library function

use std::ffi::CString;
use std::os::raw::c_char;

extern "C" {
    fn strlen(s: *const c_char) -> usize;
    fn abs(x: i32) -> i32;
}

fn main() {
    // Calling C's abs()
    let result = unsafe { abs(-42) };
    println!("{}", result); // 42

    // Calling C's strlen()
    let s = CString::new("hello").unwrap();
    let len = unsafe { strlen(s.as_ptr()) };
    println!("{}", len); // 5
}

Enter fullscreen mode Exit fullscreen mode

Exposing Rust to C

// lib.rs — compile with: rustc --crate-type=cdylib

#[no_mangle]
pub extern "C" fn add(a: i32, b: i32) -> i32 {
    a + b
}

Enter fullscreen mode Exit fullscreen mode

// main.c — link against the compiled Rust library
extern int add(int a, int b);

int main() {
    printf("%d\n", add(3, 4)); // 7
}

Enter fullscreen mode Exit fullscreen mode

Using bindgen for real C libraries

For larger C libraries, use bindgen to auto-generate Rust bindings:

cargo add bindgen --build

Enter fullscreen mode Exit fullscreen mode

// build.rs
fn main() {
    bindgen::Builder::default()
        .header("wrapper.h")
        .generate()
        .unwrap()
        .write_to_file("src/bindings.rs")
        .unwrap();
}

Enter fullscreen mode Exit fullscreen mode

💡 Most real-world FFI is wrapped in a safe Rust API. The unsafe extern calls live in a *-sys crate (e.g. openssl-sys), and a higher-level crate wraps them in a safe interface (e.g. openssl).


Wrapping Up

Here's what Part 3 covered:

Concept What it gives you
Macros Compile-time code generation, reduce boilerplate
Modules & Cargo Code organization, dependency management
Testing Built-in unit, integration, and doc tests
Higher-order functions Functional patterns, closures as values
Unsafe Rust Raw pointers and low-level control when needed
FFI Interop with C libraries and system APIs

What's in Part 4?

  • Concurrency with threads and channels
  • The Send and Sync traits
  • Mutex, RwLock, and atomics
  • Building a real CLI tool with clap

If this helped, drop a ❤️ and follow for Part 4!