Skip to content

Commit 5bfb28e

Browse files
committed
Initial state
0 parents  commit 5bfb28e

63 files changed

Lines changed: 2843 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# Rust for C++ Developers - Code Examples
2+
3+
This repository contains code examples from the book "Rust for C++ Developers" by Packt Publishing.
4+
5+
## Repository Structure
6+
7+
### Chapter 6 - Reading and Writing Files
8+
9+
The `chapter_06/` directory contains examples demonstrating:
10+
- Working with filesystem paths (`PathBuf`, `Path`)
11+
- Error handling with `Result<T, E>` and the `?` operator
12+
- High-level I/O abstractions (`read_to_string`, `read`, `write`)
13+
- Working with files using `File`, `OpenOptions`
14+
- Fundamental I/O traits (`Read`, `Write`, `Seek`)
15+
- Buffered I/O with `BufReader` and `BufWriter`
16+
- In-memory I/O with `Cursor`
17+
- Byte order handling with the `byteorder` crate
18+
- Serialization/deserialization with `serde` (JSON and TOML)
19+
20+
**Key files:**
21+
- `src/bin/paths.rs` - Path manipulation examples
22+
- `src/bin/error_handling.rs` - Error handling patterns
23+
- `src/bin/file_io.rs` - File operations
24+
- `src/bin/traits.rs` - I/O trait usage (Read, Write, Seek)
25+
- `src/bin/byteorder_example.rs` - Endianness handling
26+
- `src/bin/serde_test.rs` - JSON/TOML serialization
27+
- `pets.json` - Sample data file for serde examples
28+
- `file.txt` - Sample text file for I/O examples
29+
30+
**Running the examples:**
31+
```bash
32+
cd chapter_06
33+
34+
# Run specific examples
35+
cargo run --bin paths
36+
cargo run --bin error_handling
37+
cargo run --bin file_io
38+
cargo run --bin traits
39+
cargo run --bin byteorder_example
40+
cargo run --bin serde_test
41+
```
42+
43+
### Chapter 9 - Working with C++ in Rust (FFI)
44+
45+
The `chapter_09/` directory contains examples for Foreign Function Interface (FFI):
46+
- Manual C bindings (SDL2)
47+
- Exposing Rust functions to C
48+
- Automatic binding generation with `bindgen`
49+
- Manual C++ bindings with C API wrappers
50+
- Using the `cpp` crate for inline C++
51+
- Creating safe Rust wrappers for unsafe FFI code
52+
53+
**Key examples:**
54+
- `bind_c_manual/` - Manual SDL2 FFI bindings
55+
- `from_c/` - Creating Rust libraries callable from C
56+
- `bind_c_bindgen/` - Automatic binding generation with bindgen
57+
- `bind_cpp_manual/` - C++ bindings via C API wrapper
58+
- `bind_cpp_macros/` - Inline C++ with the cpp crate
59+
- `sdl_safe_wrapper/` - Safe abstractions over unsafe FFI
60+
61+
**Important Notes:**
62+
- Many examples require external system libraries (SDL2)
63+
- Some require C++ toolchain and C++17 support
64+
- `bind_c_bindgen` downloads and builds SDL2 automatically
65+
- See individual example READMEs for specific requirements
66+
67+
**Running the examples:**
68+
```bash
69+
cd chapter_09
70+
71+
# Simple example (Rust to C)
72+
cd from_c
73+
cargo build
74+
75+
# Examples requiring SDL2 (must be installed)
76+
cd bind_c_manual
77+
cargo run
78+
79+
cd sdl_safe_wrapper
80+
cargo run
81+
82+
# Examples with automatic builds
83+
cd bind_c_bindgen
84+
cargo build
85+
86+
cd bind_cpp_manual
87+
cargo run
88+
```
89+
90+
### Chapter 10 - Optimization in Rust
91+
92+
The `chapter_10/` directory contains a complete performance testing project demonstrating:
93+
- Matrix multiplication with multiple optimization strategies
94+
- Profiling with `samply` and flame graphs
95+
- Benchmarking with Criterion
96+
- Unsafe Rust for performance optimization
97+
- SIMD optimization (requires nightly Rust)
98+
99+
**Key files:**
100+
- `src/lib.rs` - Library with matrix multiplication implementations
101+
- `src/bin/cli.rs` - CLI tool for running performance tests
102+
- `src/simd.rs` - SIMD-optimized matrix multiplication (nightly only)
103+
- `benches/multiply.rs` - Criterion benchmarks
104+
105+
**Running the examples:**
106+
```bash
107+
cd chapter_10
108+
109+
# Build and run the CLI
110+
cargo run --release -- 100 100 100
111+
112+
# Run benchmarks
113+
cargo bench
114+
115+
# Run with nightly and SIMD (requires nightly toolchain)
116+
cargo +nightly bench --features nightly
117+
```
118+
119+
### Chapter 11 - Multithreading in Rust
120+
121+
The `chapter_11/` directory contains examples demonstrating Rust's "fearless concurrency":
122+
- Creating and managing threads with `std::thread`
123+
- Thread spawning and JoinHandle
124+
- Move closures for data ownership
125+
- Scoped threads for non-static data
126+
- Thread pooling with `rayon`
127+
- Safe shared state with `Arc<Mutex<T>>`
128+
- Parallel iterators with `rayon::prelude`
129+
- MPSC channels for thread communication
130+
- Atomic operations and lock-free programming
131+
132+
**Key files:**
133+
- `src/bin/basic_threads.rs` - Thread spawning and JoinHandle
134+
- `src/bin/move_closures.rs` - Transferring data to threads
135+
- `src/bin/scoped_threads.rs` - Scoped threads and data sharing
136+
- `src/bin/rayon_pool.rs` - Thread pooling with rayon
137+
- `src/bin/arc_mutex.rs` - Shared mutable state pattern
138+
- `src/bin/parallel_iterators.rs` - Parallel iteration with rayon
139+
- `src/bin/channels.rs` - MPSC channel communication
140+
- `src/bin/atomics.rs` - Atomic operations and CAS
141+
142+
**Running the examples:**
143+
```bash
144+
cd chapter_11
145+
146+
# Check all examples compile
147+
cargo check
148+
149+
# Run specific examples
150+
cargo run --bin basic_threads
151+
cargo run --bin move_closures
152+
cargo run --bin scoped_threads
153+
cargo run --bin rayon_pool
154+
cargo run --bin arc_mutex
155+
cargo run --bin parallel_iterators
156+
cargo run --bin channels
157+
cargo run --bin atomics
158+
```
159+
160+
### Chapter 12 - Metaprogramming with Macros
161+
162+
The `chapter_12/` directory contains examples of Rust macros:
163+
- Declarative macros (`macro_rules!`)
164+
- Procedural macros (custom derive, attributes, function-like)
165+
166+
**Key components:**
167+
- `src/main.rs` - Examples using all macro types
168+
- `prettyprint_derive/` - Custom derive macro for pretty printing structs
169+
- `log_call/` - Attribute macro for logging function calls
170+
- `stringulate/` - Function-like macro that converts tokens to strings
171+
172+
**Running the examples:**
173+
```bash
174+
cd chapter_12
175+
cargo run
176+
```
177+
178+
## Requirements
179+
180+
- Rust 1.82.0 or newer
181+
- For Chapter 10 SIMD examples: Rust nightly toolchain
182+
- For Chapter 10 profiling: `cargo install --locked samply`
183+

chapter_06/Cargo.lock

Lines changed: 196 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)