path_macro2
A cross-platform path construction macro for Rust that provides an intuitive syntax for building file paths while automatically handling platform-specific path separators.
Features
- Dual syntax support: Use either slash (
/) or comma (,) separators - Cross-platform: Automatically uses correct path separators (
\on Windows,/on Unix-like systems) - Runtime and compile-time:
path!for runtime with variable interpolation,path_const!for compile-time constants - Multiple segment types: Identifiers, dotted names, string literals, and expressions
- Zero dependencies: Lightweight macro-only implementation
Installation
Add this to your Cargo.toml:
[]
= "0.1.3"
Usage
Runtime Path Construction with path!
The path! macro creates PathBuf instances at runtime and supports variable
interpolation.
Basic Syntax
use path;
// Slash syntax
let path1 = path!;
// Comma syntax
let path2 = path!;
// Both produce the same result:
// Windows: "vendor\dll\windivert.c"
// Unix: "vendor/dll/windivert.c"
Supported Segment Types
Identifiers and Dotted Names
use path;
let path = path!; // Simple identifiers
let file = path!; // Dotted identifiers
String Literals (for spaces and special characters)
use path;
let path = path!;
let docs = path!;
Variable Interpolation
use path;
let base = "vendor";
let version = "1.0";
// Variables wrapped in curly braces
let path = path!;
let versioned = path!;
Platform-Specific Examples
Unix/Linux Absolute Paths
use path;
let abs_path = path!;
// Result: "/usr/local/bin/myapp"
Windows Paths
use path;
// Drive letter paths
let win_path = path!;
// Result: "C:\Program Files\MyApp\app.exe"
// UNC network paths
let unc_path = path!;
// Result: "\\server\share\file.txt"
Compile-Time Path Constants with path_const!
The path_const! macro generates compile-time string constants, perfect for use
with concat! and in const contexts.
Basic Usage
use path_const;
// Compile-time constants
const CONFIG_PATH: &str = path_const!;
const LIB_PATH: &str = path_const!;
// Results:
// Windows: "config\\app.toml", "vendor\\dll\\windivert.c"
// Unix: "config/app.toml", "vendor/dll/windivert.c"
Combining with concat! for Build Flags
use path_const;
// Perfect for build scripts and compiler flags
const DEF_FLAG: &str = concat!;
const INCLUDE_FLAG: &str = concat!;
// Use in arrays
const BUILD_ARGS: & = &;
String Literals and Dotted Identifiers
use path_const;
// Handles spaces and special characters
const DOC_PATH: &str = path_const!;
const MIXED_PATH: &str = path_const!;
// Dotted identifiers work seamlessly
const SOURCE_FILE: &str = path_const!;
const ARCHIVE: &str = path_const!;
Complex Examples
Runtime Path Construction
use path;
Build Script with Compile-Time Paths
use path_const;
// build.rs
const DYNAMIC_CL_ARGS: & = &;
How It Works
path! Macro (Runtime)
The path! macro processes path segments and automatically:
- Converts identifiers to strings:
vendorbecomes"vendor" - Handles dotted identifiers:
file.txtbecomes"file.txt" - Preserves string literals:
"my folder"stays as-is - Evaluates expressions:
{base_path}evaluates the variable - Builds PathBuf: Uses
std::path::PathBuf::push()for proper platform handling
The result is always a std::path::PathBuf that uses the correct path
separators for the target platform.
path_const! Macro (Compile-Time)
The path_const! macro generates compile-time string constants:
- Converts identifiers to strings:
vendorbecomes"vendor" - Handles dotted identifiers:
file.txtbecomes"file.txt" - Preserves string literals:
"my folder"stays as-is - Joins with platform separators: Uses
concat!for zero-runtime-cost - No variable support: Only literals and identifiers (use
path!for variables)
The result is always a &'static str with platform-appropriate separators.
Comparison with Alternatives
| Method | Cross-platform | Readable | Variables | Compile-time | Runtime |
|---|---|---|---|---|---|
path_macro2::path! |
✅ | ✅ | ✅ | ❌ | ✅ |
path_macro2::path_const! |
✅ | ✅ | ❌ | ✅ | ✅ |
std::path::Path::join() |
✅ | ❌ | ✅ | ❌ | ✅ |
| String concatenation | ❌ | ❌ | ✅ | ✅ | ✅ |
format!() with / |
❌ | ⚠️ | ✅ | ❌ | ✅ |
concat!() with / |
❌ | ⚠️ | ❌ | ✅ | ✅ |
When to Use Which Macro
-
Use
path!when you need:- Runtime path construction
- Variable interpolation
PathBufinstances- Dynamic path building
-
Use
path_const!when you need:- Compile-time constants
- Build script configurations
- Static path definitions
- Integration with
concat!macro - Zero runtime overhead
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.