Files
learning-zig-rus/src/ex-ch06-02.zig
zed bedda3b028 On branch main
modified:   src/ch06.md
new file:   src/ex-ch06-01.zig
new file:   src/ex-ch06-02.zig
2023-11-16 12:05:03 +03:00

31 lines
662 B
Zig

const std = @import("std");
pub fn main() !void {
// again, we'll talk about allocators soon!
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
// create a User on the heap
var user = try allocator.create(User);
// free the memory allocated for the user at the end of this scope
defer allocator.destroy(user);
user.id = 1;
user.power = 100;
// this line has been added
levelUp(user);
std.debug.print("User {d} has power of {d}\n", .{user.id, user.power});
}
fn levelUp(user: *User) void {
user.power += 1;
}
pub const User = struct {
id: u64,
power: i32,
};