This repository contains a collection of examples demonstrating various features and capabilities of the Zig programming language (version 0.14.0).
These examples showcase Zig's powerful features, from basic memory management to advanced compile-time metaprogramming. Each example is self-contained and comes with explanatory comments.
- Zig 0.14.0 or compatible version
- Basic familiarity with programming concepts
Demonstrates Zig's various memory allocation strategies:
- General Purpose Allocator
- Arena Allocator
- Fixed Buffer Allocator
- Page Allocator
zig run 01_memory_allocators.zig
Showcases common data structures in Zig's standard library:
- ArrayList
- StringHashMap
- AutoHashMap
- BufSet
- SinglyLinkedList
zig run 02_data_structures.zig
Explores string manipulation and formatting capabilities:
- String concatenation
- Formatting with placeholders
- Parsing strings
zig run 03_string_formatting.zig
Demonstrates file operations in Zig:
- Reading and writing files
- Directory operations
- Path manipulation
zig run 04_file_io.zig
Showcases Zig's error handling mechanisms:
- Error sets
- Try/catch
- Error unions
- Error return tracing
zig run 05_error_handling.zig
Demonstrates Zig's built-in testing framework:
- Unit tests
- Test expectation functions
- Test setup and teardown
zig run 06_testing.zig
# or
zig test 06_testing.zig
Shows threading and concurrency in Zig:
- Thread creation
- Mutexes
- Condition variables
- Thread pools
zig run 07_threading.zig
Demonstrates JSON parsing and generation:
- Parsing JSON strings
- Generating JSON from data
- Working with JSON objects and arrays
zig run 08_json.zig
Implements basic networking functionality:
- TCP server and client
- Socket programming
- Asynchronous I/O
zig run 09_networking.zig
Explores Zig's powerful compile-time metaprogramming:
- Compile-time function execution
- Type reflection
- Generic data structures
- Type-based specialization
zig run 10_comptime_features.zig
Shows cryptographic capabilities:
- Hash functions (SHA-256, SHA-512, Blake2, Blake3)
- HMAC for message authentication
- Password hashing
- Symmetric encryption
- Secure random number generation
zig run 11_cryptography.zig
Demonstrates Zig's build system for larger projects:
- Multi-file projects
- Dependencies
- Custom build steps
- Testing integration
cd 12_build_system
zig build
zig build run
zig build test
Shows Zig's seamless interoperability with C:
- Calling C functions from Zig
- Working with C structs
- Memory management across the FFI boundary
- String conversion between C and Zig
cd 13_ffi
zig build run
Two key documentation files are included:
overview.md
- A comprehensive overview of Zig's standard libraryadditional.md
- Additional reference material for the Zig language
- Manual memory management with advanced allocator design
- Compile-time metaprogramming
- Error handling as a first-class language feature
- C ABI compatibility and seamless FFI
- Performance and safety focused design
- Cross-compilation capabilities
- Powerful build system
Feel free to add more examples or improve existing ones!
This project is available under the MIT License - see the LICENSE file for details.