Skip to content

Instantly share code, notes, and snippets.

@hnakamur
Created February 17, 2023 23:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hnakamur/2f7f7e852ac2b00b12affe0d14132856 to your computer and use it in GitHub Desktop.
Save hnakamur/2f7f7e852ac2b00b12affe0d14132856 to your computer and use it in GitHub Desktop.
Zig BoundedArray example
// Zig version: 0.10.1
// Import the standard library.
const std = @import("std");
// Define MyBoundedArray type.
const maxLength = 32;
const MyBoundedArray = std.BoundedArray(u8, maxLength);
/// Takes a slice as a parameter and fills it with a message.
fn zigBits() MyBoundedArray {
// Create an array literal.
var message = [_]u8{ 'z', 'i', 'g', 'b', 'i', 't', 's' };
// Print the array as string.
std.log.debug("{s}", .{message});
// Make an bounded array.
var array = MyBoundedArray.init(message.len) catch unreachable;
std.mem.copy(u8, array.slice(), &message);
return array;
}
/// Entrypoint of the program.
pub fn main() void {
// Get the message.
var message = zigBits();
// Print the message.
std.log.debug("{s}", .{message.slice()});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment