This repository contains code examples from the book "Rust for C++ Developers" by Packt Publishing.
The chapter_06/ directory contains examples demonstrating:
- Working with filesystem paths (
PathBuf,Path) - Error handling with
Result<T, E>and the?operator - High-level I/O abstractions (
read_to_string,read,write) - Working with files using
File,OpenOptions - Fundamental I/O traits (
Read,Write,Seek) - Buffered I/O with
BufReaderandBufWriter - In-memory I/O with
Cursor - Byte order handling with the
byteordercrate - Serialization/deserialization with
serde(JSON and TOML)
Key files:
src/bin/paths.rs- Path manipulation examplessrc/bin/error_handling.rs- Error handling patternssrc/bin/file_io.rs- File operationssrc/bin/traits.rs- I/O trait usage (Read, Write, Seek)src/bin/byteorder_example.rs- Endianness handlingsrc/bin/serde_test.rs- JSON/TOML serializationpets.json- Sample data file for serde examplesfile.txt- Sample text file for I/O examples
Running the examples:
cd chapter_06
# Run specific examples
cargo run --bin paths
cargo run --bin error_handling
cargo run --bin file_io
cargo run --bin traits
cargo run --bin byteorder_example
cargo run --bin serde_testThe chapter_09/ directory contains examples for Foreign Function Interface (FFI):
- Manual C bindings (SDL2)
- Exposing Rust functions to C
- Automatic binding generation with
bindgen - Manual C++ bindings with C API wrappers
- Using the
cppcrate for inline C++ - Creating safe Rust wrappers for unsafe FFI code
Key examples:
bind_c_manual/- Manual SDL2 FFI bindingsfrom_c/- Creating Rust libraries callable from Cbind_c_bindgen/- Automatic binding generation with bindgenbind_cpp_manual/- C++ bindings via C API wrapperbind_cpp_macros/- Inline C++ with the cpp cratesdl_safe_wrapper/- Safe abstractions over unsafe FFI
Important Notes:
- Many examples require external system libraries (SDL2)
- Some require C++ toolchain and C++17 support
bind_c_bindgendownloads and builds SDL2 automatically- See individual example READMEs for specific requirements
Running the examples:
cd chapter_09
# Simple example (Rust to C)
cd from_c
cargo build
# Examples requiring SDL2 (must be installed)
cd bind_c_manual
cargo run
cd sdl_safe_wrapper
cargo run
# Examples with automatic builds
cd bind_c_bindgen
cargo build
cd bind_cpp_manual
cargo runThe chapter_10/ directory contains a complete performance testing project demonstrating:
- Matrix multiplication with multiple optimization strategies
- Profiling with
samplyand flame graphs - Benchmarking with Criterion
- Unsafe Rust for performance optimization
- SIMD optimization (requires nightly Rust)
Key files:
src/lib.rs- Library with matrix multiplication implementationssrc/bin/cli.rs- CLI tool for running performance testssrc/simd.rs- SIMD-optimized matrix multiplication (nightly only)benches/multiply.rs- Criterion benchmarks
Running the examples:
cd chapter_10
# Build and run the CLI
cargo run --release -- 100 100 100
# Run benchmarks
cargo bench
# Run with nightly and SIMD (requires nightly toolchain)
cargo +nightly bench --features nightlyThe chapter_11/ directory contains examples demonstrating Rust's "fearless concurrency":
- Creating and managing threads with
std::thread - Thread spawning and JoinHandle
- Move closures for data ownership
- Scoped threads for non-static data
- Thread pooling with
rayon - Safe shared state with
Arc<Mutex<T>> - Parallel iterators with
rayon::prelude - MPSC channels for thread communication
- Atomic operations and lock-free programming
Key files:
src/bin/basic_threads.rs- Thread spawning and JoinHandlesrc/bin/move_closures.rs- Transferring data to threadssrc/bin/scoped_threads.rs- Scoped threads and data sharingsrc/bin/rayon_pool.rs- Thread pooling with rayonsrc/bin/arc_mutex.rs- Shared mutable state patternsrc/bin/parallel_iterators.rs- Parallel iteration with rayonsrc/bin/channels.rs- MPSC channel communicationsrc/bin/atomics.rs- Atomic operations and CAS
Running the examples:
cd chapter_11
# Check all examples compile
cargo check
# Run specific examples
cargo run --bin basic_threads
cargo run --bin move_closures
cargo run --bin scoped_threads
cargo run --bin rayon_pool
cargo run --bin arc_mutex
cargo run --bin parallel_iterators
cargo run --bin channels
cargo run --bin atomicsThe chapter_12/ directory contains examples of Rust macros:
- Declarative macros (
macro_rules!) - Procedural macros (custom derive, attributes, function-like)
Key components:
src/main.rs- Examples using all macro typesprettyprint_derive/- Custom derive macro for pretty printing structslog_call/- Attribute macro for logging function callsstringulate/- Function-like macro that converts tokens to strings
Running the examples:
cd chapter_12
cargo run- Rust 1.82.0 or newer
- For Chapter 10 SIMD examples: Rust nightly toolchain
- For Chapter 10 profiling:
cargo install --locked samply