Created
February 17, 2023 23:55
-
-
Save hnakamur/2f7f7e852ac2b00b12affe0d14132856 to your computer and use it in GitHub Desktop.
Zig BoundedArray example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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