// Single line comment
/// Doc comment for documentation generation
// Import standard library
conststd=@import("std");// Declare constants
constMY_CONSTANT:i32=42;constPI=3.14159;// Declare an enum
constDirection=enum{north,south,east,west,// Enum with method
pubfnisVertical(self:Direction)bool{returnself== .northorself== .south;}};// Define a struct
constPerson=struct{name: []constu8,age:u8,height:f32,// Method definition
pubfnintroduce(self:Person)void{std.debug.print("Hi, I'm {s}, {d} years old\n", .{self.name,self.age,});}};// Error set definition
constMathError=error{DivisionByZero,Overflow,};// Union type
constValue=union(enum){integer:i64,float:f64,boolean:bool,};// Main function
pubfnmain()!void{// Variables
varmutable_var:i32=100;constimmutable_var=@as(i32,200);// Array
vararray= [_]i32{1,2,3,4,5};// Slice
constslice=array[1..3];// String literal
constgreeting="Hello, Zig!";// Optional type
varoptional_value:?i32=null;optional_value=42;// Error union type
varresult:MathError!i32=undefined;// If statement
if(mutable_var>50){std.debug.print("Greater than 50\n", .{});}else{std.debug.print("Less than or equal to 50\n", .{});}// Switch statement
constdirection=Direction.north;switch(direction){ .north=>std.debug.print("Going north\n", .{}), .south=>std.debug.print("Going south\n", .{}),else=>std.debug.print("Going east or west\n", .{}),}// While loop
vari:usize=0;while(i<5) : (i+=1){if(i==2)continue;if(i==4)break;std.debug.print("{d}\n", .{i});}// For loop
for(array, 0..)|value,index|{std.debug.print("Index: {d}, Value: {d}\n", .{index,value});}// Error handling
result=divide(10,2)catch|err|{std.debug.print("Error: {}\n", .{err});returnerr;};// Defer statement
{constfile=std.fs.cwd().createFile("test.txt", .{ .read=true},)catch|err|{std.debug.print("Could not create file: {}\n", .{err});return;};deferfile.close();}// Allocator example
vargpa=std.heap.GeneralPurposeAllocator(.{}){};defer_=gpa.deinit();constallocator=gpa.allocator();// Dynamic array (ArrayList)
varlist=std.ArrayList(i32).init(allocator);deferlist.deinit();trylist.append(42);// Comptime
constcompiled_value=comptimeblk: {varx:i32=1;x+=2;break :blkx;};// Testing
if(std.builtin.is_test){trystd.testing.expect(compiled_value==3);}}// Function with error handling
fndivide(a:i32,b:i32)MathError!i32{if(b==0)returnMathError.DivisionByZero;returna/b;}// Test function
test"basic test"{trystd.testing.expect(MY_CONSTANT==42);}