#handle enum from in error

1 messages · Page 1 of 1 (latest)

mystic cliff
#

example code

const std = @import("std");

const GeneralPurposeAllocator = std.heap.GeneralPurposeAllocator(.{});

const MyEnum = enum(i64) {
    a = 1,
    b = 2,
};

pub fn main() !void {
    var alloc: GeneralPurposeAllocator = .{};
    const args = try std.process.argsAlloc(alloc.allocator());

    if (args.len < 2) {
        std.debug.panic("require at least one argument", .{});
    }

    const value = try std.fmt.parseInt(i64, args[1], 10);

    const enumval: MyEnum = @enumFromInt(value);

    std.debug.print("{}", .{enumval});
}
#

can i use catch block?

#

problem solved, there is std.meta.intToEnum
IDK how to mark this thread as solved.