Skip to content

Commit

Permalink
exercises: examples: improve ArrayList deinits (#318)
Browse files Browse the repository at this point in the history
Use errdefer where we later use toOwnedSlice.

Refs: #313
  • Loading branch information
ee7 authored Sep 19, 2023
1 parent 4b639d5 commit cea27d4
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion exercises/practice/acronym/.meta/example.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const ascii = std.ascii;

pub fn abbreviate(allocator: mem.Allocator, words: []const u8) mem.Allocator.Error![]u8 {
var letters = std.ArrayList(u8).init(allocator);
defer letters.deinit();
errdefer letters.deinit();

for (0..words.len) |i| {
if (!ascii.isAlphabetic(words[i])) continue;
Expand Down
1 change: 1 addition & 0 deletions exercises/practice/all-your-base/.meta/example.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fn toBase10(digits: []const u32, input_base: u32) u32 {

fn fromBase10(allocator: mem.Allocator, num: u32, output_base: u32) mem.Allocator.Error![]u32 {
var list = std.ArrayList(u32).init(allocator);
errdefer list.deinit();
var n = num;
while (n > 0) {
try list.append(n % output_base);
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/secret-handshake/.meta/example.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub const Signal = enum(u2) {

pub fn calculateHandshake(allocator: mem.Allocator, number: isize) mem.Allocator.Error![]const Signal {
var list = std.ArrayList(Signal).init(allocator);
defer list.deinit();
errdefer list.deinit();
if (isFlipped(number, 0b1)) {
try list.append(Signal.wink);
}
Expand Down

0 comments on commit cea27d4

Please sign in to comment.