Universal Code Snippet
const std = @import("std");
pub fn main() void {
std.debug.print("Hello, World!\n", .{});
}const std = @import("std");
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
const stdin = std.io.getStdIn().reader();
var buf: [100]u8 = undefined;
try stdout.print("Enter your name: ", .{});
const name = (try stdin.readUntilDelimiterOrEof(&buf, '\n')) orelse "";
try stdout.print("Hello, {s}!\n", .{name});
}const std = @import("std");
pub fn main() void {
const age: i32 = 20;
if (age >= 18) {
std.debug.print("You are an adult.\n", .{});
} else {
std.debug.print("You are a minor.\n", .{});
}
}const std = @import("std");
pub fn main() void {
std.debug.print("For loop:\n", .{});
for (1..6) |i| {
std.debug.print("{} ", .{i});
}
std.debug.print("\n", .{});
std.debug.print("While loop:\n", .{});
var j: i32 = 1;
while (j <= 5) : (j += 1) {
std.debug.print("{} ", .{j});
}
std.debug.print("\n", .{});
}const std = @import("std");
fn add(a: i32, b: i32) i32 {
return a + b;
}
pub fn main() void {
std.debug.print("Sum: {}\n", .{add(10, 20)});
}const std = @import("std");
pub fn main() void {
const numbers = [_]i32{ 10, 20, 30, 40, 50 };
for (numbers, 0..) |v, i| {
std.debug.print("Element {}: {}\n", .{ i, v });
}
}const std = @import("std");
pub fn main() void {
const str1 = "Hello";
const str2 = " World";
std.debug.print("Length: {}\n", .{str1.len});
std.debug.print("Concatenated: {s}{s}\n", .{ str1, str2 });
std.debug.print("Uppercase: {s}\n", .{std.ascii.upperString(@constCast(str1), str1)});
}const std = @import("std");
pub fn main() void {
const a: f64 = 15.0;
const b: f64 = 4.0;
std.debug.print("Add: {d}\n", .{a + b});
std.debug.print("Subtract: {d}\n", .{a - b});
std.debug.print("Multiply: {d}\n", .{a * b});
std.debug.print("Divide: {d}\n", .{a / b});
}