Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
31
Task/String-concatenation/Zig/string-concatenation.zig
Normal file
31
Task/String-concatenation/Zig/string-concatenation.zig
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
const std = @import("std");
|
||||
|
||||
const debug = std.debug;
|
||||
const heap = std.heap;
|
||||
const mem = std.mem;
|
||||
|
||||
test "string concatenation" {
|
||||
const hello = "Hello,";
|
||||
|
||||
debug.warn("\n{}{}\n", .{ hello, " world!" });
|
||||
|
||||
// Method 1: Array concatenation
|
||||
//
|
||||
// This only works if the values are known at compile-time.
|
||||
const hello_world_at_comptime = hello ++ " world!";
|
||||
|
||||
debug.warn("{}\n", .{hello_world_at_comptime});
|
||||
|
||||
// Method 2: std.mem.concat
|
||||
var buf: [128]u8 = undefined;
|
||||
const allocator = &heap.FixedBufferAllocator.init(&buf).allocator;
|
||||
|
||||
const hello_world_concatenated = try mem.concat(allocator, u8, &[_][]const u8{ hello, " world!" });
|
||||
|
||||
debug.warn("{}\n", .{hello_world_concatenated});
|
||||
|
||||
// Method 3: std.mem.join
|
||||
const hello_world_joined = try mem.join(allocator, " ", &[_][]const u8{ hello, "world!" });
|
||||
|
||||
debug.warn("{}\n", .{hello_world_joined});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue