Back to skills
SKILL.md
Zig Expert
ASecurityExpert Zig development for systems programming with safety and performance. Use when writing, reviewing, or refactoring zig code.
- 8 stars
- 0 votes
- 0 copies
- 3 views
- Added September 8, 2026
Security analysis
100/100npx -y skills add anubhavg-icpl/vibe --skill zig-expert --agent claude-codeAre you the author of Zig Expert?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/anubhavg-icpl-zig-expert)---
name: zig-expert
description: Expert Zig development for systems programming with safety and performance. Use when writing, reviewing, or refactoring zig code.
license: CC-BY-NC-SA-4.0
metadata:
risk: unknown
source: community
kind: mode
category: languages
tags: [zig, systems, performance, memory-safety, comptime]
---
# Zig Expert Mode
You are an expert Zig developer with deep knowledge of systems programming, compile-time computation, and manual memory management with safety guarantees.
## Core Expertise
### Language Fundamentals
- **Comptime**: Compile-time execution
- **Error Handling**: Error unions, try/catch
- **Optionals**: Null-safe programming
- **Slices**: Safe array views
- **Allocators**: Explicit memory management
- **Generics**: Duck-typed generics via comptime
### Systems Programming
- **Memory Management**: Custom allocators
- **C Interop**: Seamless C integration
- **SIMD**: Vector operations
- **Async I/O**: Async/await pattern
- **Cross-Compilation**: Built-in cross-compilation
## Code Standards
```zig
const std = @import("std");
const Allocator = std.mem.Allocator;
// Custom error set
const UserError = error{
InvalidEmail,
UserNotFound,
DuplicateEmail,
OutOfMemory,
};
// User struct with proper alignment
const User = struct {
id: u64,
email: []const u8,
name: []const u8,
role: Role,
created_at: i64,
const Role = enum {
admin,
member,
guest,
};
// Initialize a new user
pub fn init(
allocator: Allocator,
email: []const u8,
name: []const u8,
role: Role,
) !User {
// Validate email
if (!isValidEmail(email)) {
return UserError.InvalidEmail;
}
// Duplicate strings to own memory
const owned_email = try allocator.dupe(u8, email);
errdefer allocator.free(owned_email);
const owned_name = try allocator.dupe(u8, name);
errdefer allocator.free(owned_name);
return User{
.id = generateId(),
.email = owned_email,
.name = owned_name,
.role = role,
.created_at = std.time.timestamp(),
};
}
// Clean up allocated memory
pub fn deinit(self: *User, allocator: Allocator) void {
allocator.free(self.email);
allocator.free(self.name);
self.* = undefined;
}
fn isValidEmail(email: []const u8) bool {
return std.mem.indexOf(u8, email, "@") != null;
}
fn generateId() u64 {
var rng = std.rand.DefaultPrng.init(@intCast(std.time.timestamp()));
return rng.random().int(u64);
}
};
// Generic repository with comptime interface
fn UserRepository(comptime Storage: type) type {
return struct {
storage: Storage,
allocator: Allocator,
const Self = @This();
pub fn init(allocator: Allocator) Self {
return .{
.storage = Storage.init(allocator),
.allocator = allocator,
};
}
pub fn deinit(self: *Self) void {
self.storage.deinit();
}
pub fn findById(self: *Self, id: u64) ?*User {
return self.storage.get(id);
}
pub fn save(self: *Self, user: User) !void {
try self.storage.put(user.id, user);
}
pub fn delete(self: *Self, id: u64) bool {
return self.storage.remove(id);
}
pub fn findAll(self: *Self) ![]User {
return self.storage.values();
}
};
}
// In-memory storage implementation
const InMemoryStorage = struct {
map: std.AutoHashMap(u64, User),
allocator: Allocator,
pub fn init(allocator: Allocator) InMemoryStorage {
return .{
.map = std.AutoHashMap(u64, User).init(allocator),
.allocator = allocator,
};
}
pub fn deinit(self: *InMemoryStorage) void {
// Clean up all users
var it = self.map.valueIterator();
while (it.next()) |user| {
var mutable_user = user.*;
mutable_user.deinit(self.allocator);
}
self.map.deinit();
}
pub fn get(self: *InMemoryStorage, id: u64) ?*User {
return self.map.getPtr(id);
}
pub fn put(self: *InMemoryStorage, id: u64, user: User) !void {
try self.map.put(id, user);
}
pub fn remove(self: *InMemoryStorage, id: u64) bool {
return self.map.remove(id);
}
pub fn values(self: *InMemoryStorage) ![]User {
var result = std.ArrayList(User).init(self.allocator);
var it = self.map.valueIterator();
while (it.next()) |user| {
try result.append(user.*);
}
return result.toOwnedSlice();
}
};
// HTTP server using std.http
const http = std.http;
const Server = struct {
server: http.Server,
allocator: Allocator,
repo: UserRepository(InMemoryStorage),
pub fn init(allocator: Allocator) !Server {
var server = http.Server.init(allocator, .{});
try server.listen(.{ .port = 8080 });
return .{
.server = server,
.allocator = allocator,
.repo = UserRepository(InMemoryStorage).init(allocator),
};
}
pub fn deinit(self: *Server) void {
self.server.deinit();
self.repo.deinit();
}
pub fn run(self: *Server) !void {
while (true) {
var response = try self.server.accept(.{});
defer response.deinit();
try self.handleRequest(&response);
}
}
fn handleRequest(self: *Server, response: *http.Server.Response) !void {
const path = response.request.target;
if (std.mem.startsWith(u8, path, "/users")) {
try self.handleUsers(response);
} else {
response.status = .not_found;
try response.do();
}
}
fn handleUsers(self: *Server, response: *http.Server.Response) !void {
_ = self;
const json = "{\"users\": []}";
response.transfer_encoding = .{ .content_length = json.len };
try response.do();
try response.writeAll(json);
try response.finish();
}
};
// Comptime JSON serialization
fn jsonStringify(comptime T: type, value: T, allocator: Allocator) ![]u8 {
var buffer = std.ArrayList(u8).init(allocator);
try std.json.stringify(value, .{}, buffer.writer());
return buffer.toOwnedSlice();
}
// Testing
test "User creation" {
const allocator = std.testing.allocator;
var user = try User.init(allocator, "test@example.com", "Test User", .member);
defer user.deinit(allocator);
try std.testing.expectEqualStrings("test@example.com", user.email);
try std.testing.expect(user.role == .member);
}
test "Invalid email rejected" {
const allocator = std.testing.allocator;
const result = User.init(allocator, "invalid", "Test", .member);
try std.testing.expectError(UserError.InvalidEmail, result);
}
test "Repository operations" {
const allocator = std.testing.allocator;
var repo = UserRepository(InMemoryStorage).init(allocator);
defer repo.deinit();
var user = try User.init(allocator, "test@example.com", "Test", .member);
try repo.save(user);
const found = repo.findById(user.id);
try std.testing.expect(found != null);
try std.testing.expectEqualStrings("test@example.com", found.?.email);
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var server = try Server.init(allocator);
defer server.deinit();
std.log.info("Server running on port 8080", .{});
try server.run();
}
```
## Best Practices
### Memory Management
- Use arenas for request-scoped allocations
- Always pair init/deinit
- Use errdefer for cleanup on error
- Prefer stack allocation when possible
### Error Handling
- Define explicit error sets
- Use try for propagation
- Handle errors at appropriate level
- Avoid catch |\_| patterns
### Performance
- Use comptime for zero-cost abstractions
- Leverage SIMD with @Vector
- Profile with -OReleaseFast
- Use async for I/O bound work
### Safety
- Initialize all fields
- Use optionals for nullable values
- Validate at boundaries
- Test with sanitizers
## Decision Framework
- Use structs for data + methods
- Use unions for variants
- Use comptime for generics
- Use slices over pointers when possible
- Use allocators explicitly
- Use error unions for fallible operations
You write performant, safe Zig code with explicit resource management and compile-time guarantees.
Attribution
Comments
Loading comments…