my ziglings
0

Configure Feed

Select the types of activity you want to include in your feed.

Converted var to const if there is no mutation in var. This is checked from compiler version 0.12.0-dev.1664

Chris Boesch (Nov 21, 2023, 3:01 PM +0100) 7679f93f b7015c2d

+46 -46
+1 -1
exercises/025_errors5.zig
··· 26 26 // This function needs to return any error which might come back from detect(). 27 27 // Please use a "try" statement rather than a "catch". 28 28 // 29 - var x = detect(n); 29 + const x = detect(n); 30 30 31 31 return x + 5; 32 32 }
+2 -2
exercises/029_errdefer.zig
··· 21 21 22 22 pub fn main() void { 23 23 // We simply quit the entire program if we fail to get a number: 24 - var a: u32 = makeNumber() catch return; 25 - var b: u32 = makeNumber() catch return; 24 + const a: u32 = makeNumber() catch return; 25 + const b: u32 = makeNumber() catch return; 26 26 27 27 std.debug.print("Numbers: {}, {}\n", .{ a, b }); 28 28 }
+1 -1
exercises/039_pointers.zig
··· 24 24 25 25 pub fn main() void { 26 26 var num1: u8 = 5; 27 - var num1_pointer: *u8 = &num1; 27 + const num1_pointer: *u8 = &num1; 28 28 29 29 var num2: u8 = undefined; 30 30
+1 -1
exercises/048_methods2.zig
··· 24 24 25 25 pub fn print(self: *Elephant) void { 26 26 // Prints elephant letter and [v]isited 27 - var v: u8 = if (self.visited) 'v' else ' '; 27 + const v: u8 = if (self.visited) 'v' else ' '; 28 28 std.debug.print("{u}{u} ", .{ self.letter, v }); 29 29 } 30 30 };
+1 -1
exercises/049_quiz6.zig
··· 37 37 38 38 pub fn print(self: *Elephant) void { 39 39 // Prints elephant letter and [v]isited 40 - var v: u8 = if (self.visited) 'v' else ' '; 40 + const v: u8 = if (self.visited) 'v' else ' '; 41 41 std.debug.print("{u}{u} ", .{ self.letter, v }); 42 42 } 43 43 };
+2 -2
exercises/055_unions.zig
··· 53 53 54 54 pub fn main() void { 55 55 // We'll just make one bee and one ant to test them out: 56 - var ant = Insect{ .still_alive = true }; 57 - var bee = Insect{ .flowers_visited = 15 }; 56 + const ant = Insect{ .still_alive = true }; 57 + const bee = Insect{ .flowers_visited = 15 }; 58 58 59 59 std.debug.print("Insect report! ", .{}); 60 60
+2 -2
exercises/056_unions2.zig
··· 38 38 }; 39 39 40 40 pub fn main() void { 41 - var ant = Insect{ .still_alive = true }; 42 - var bee = Insect{ .flowers_visited = 16 }; 41 + const ant = Insect{ .still_alive = true }; 42 + const bee = Insect{ .flowers_visited = 16 }; 43 43 44 44 std.debug.print("Insect report! ", .{}); 45 45
+2 -2
exercises/057_unions3.zig
··· 21 21 }; 22 22 23 23 pub fn main() void { 24 - var ant = Insect{ .still_alive = true }; 25 - var bee = Insect{ .flowers_visited = 17 }; 24 + const ant = Insect{ .still_alive = true }; 25 + const bee = Insect{ .flowers_visited = 17 }; 26 26 27 27 std.debug.print("Insect report! ", .{}); 28 28
+2 -2
exercises/058_quiz7.zig
··· 273 273 // distance) than the one we'd noted before. If it is, we 274 274 // overwrite the old entry with the new one. 275 275 fn checkNote(self: *HermitsNotebook, note: NotebookEntry) void { 276 - var existing_entry = self.getEntry(note.place); 276 + const existing_entry = self.getEntry(note.place); 277 277 278 278 if (existing_entry == null) { 279 279 self.entries[self.end_of_entries] = note; ··· 386 386 // "start" entry we just added) until we run out, at which point 387 387 // we'll have checked every reachable Place. 388 388 while (notebook.hasNextEntry()) { 389 - var place_entry = notebook.getNextEntry(); 389 + const place_entry = notebook.getNextEntry(); 390 390 391 391 // For every Path that leads FROM the current Place, create a 392 392 // new note (in the form of a NotebookEntry) with the
+4 -4
exercises/067_comptime2.zig
··· 38 38 var count = 0; 39 39 40 40 count += 1; 41 - var a1: [count]u8 = .{'A'} ** count; 41 + const a1: [count]u8 = .{'A'} ** count; 42 42 43 43 count += 1; 44 - var a2: [count]u8 = .{'B'} ** count; 44 + const a2: [count]u8 = .{'B'} ** count; 45 45 46 46 count += 1; 47 - var a3: [count]u8 = .{'C'} ** count; 47 + const a3: [count]u8 = .{'C'} ** count; 48 48 49 49 count += 1; 50 - var a4: [count]u8 = .{'D'} ** count; 50 + const a4: [count]u8 = .{'D'} ** count; 51 51 52 52 print("{s} {s} {s} {s}\n", .{ a1, a2, a3, a4 }); 53 53
+3 -3
exercises/070_comptime5.zig
··· 83 83 84 84 pub fn main() void { 85 85 // This is a real duck! 86 - var ducky1 = Duck{ 86 + const ducky1 = Duck{ 87 87 .eggs = 0, 88 88 .loudness = 3, 89 89 }; 90 90 91 91 // This is not a real duck, but it has quack() and waddle() 92 92 // abilities, so it's still a "duck". 93 - var ducky2 = RubberDuck{ 93 + const ducky2 = RubberDuck{ 94 94 .in_bath = false, 95 95 }; 96 96 97 97 // This is not even remotely a duck. 98 - var ducky3 = Duct{ 98 + const ducky3 = Duct{ 99 99 .diameter = 17, 100 100 .length = 165, 101 101 .galvanized = true,
+1 -1
exercises/072_comptime7.zig
··· 39 39 40 40 // This gets the digit from the "instruction". Can you 41 41 // figure out why we subtract '0' from it? 42 - comptime var digit = instructions[i + 1] - '0'; 42 + const digit = instructions[i + 1] - '0'; 43 43 44 44 // This 'switch' statement contains the actual work done 45 45 // at runtime. At first, this doesn't seem exciting...
+2 -2
exercises/075_quiz8.zig
··· 110 110 } 111 111 112 112 fn checkNote(self: *HermitsNotebook, note: NotebookEntry) void { 113 - var existing_entry = self.getEntry(note.place); 113 + const existing_entry = self.getEntry(note.place); 114 114 115 115 if (existing_entry == null) { 116 116 self.entries[self.end_of_entries] = note; ··· 180 180 notebook.checkNote(working_note); 181 181 182 182 while (notebook.hasNextEntry()) { 183 - var place_entry = notebook.getNextEntry(); 183 + const place_entry = notebook.getNextEntry(); 184 184 185 185 for (place_entry.place.paths) |*path| { 186 186 working_note = NotebookEntry{
+1 -1
exercises/076_sentinels.zig
··· 46 46 var nums = [_:0]u32{ 1, 2, 3, 4, 5, 6 }; 47 47 48 48 // And here's a zero-terminated many-item pointer: 49 - var ptr: [*:0]u32 = &nums; 49 + const ptr: [*:0]u32 = &nums; 50 50 51 51 // For fun, let's replace the value at position 3 with the 52 52 // sentinel value 0. This seems kind of naughty.
+2 -2
exercises/080_anonymous_structs.zig
··· 48 48 // * circle1 should hold i32 integers 49 49 // * circle2 should hold f32 floats 50 50 // 51 - var circle1 = ??? { 51 + const circle1 = ??? { 52 52 .center_x = 25, 53 53 .center_y = 70, 54 54 .radius = 15, 55 55 }; 56 56 57 - var circle2 = ??? { 57 + const circle2 = ??? { 58 58 .center_x = 25.234, 59 59 .center_y = 70.999, 60 60 .radius = 15.714,
+1 -1
exercises/092_interfaces.zig
··· 96 96 }; 97 97 98 98 pub fn main() !void { 99 - var my_insects = [_]Insect{ 99 + const my_insects = [_]Insect{ 100 100 Insect{ .ant = Ant{ .still_alive = true } }, 101 101 Insect{ .bee = Bee{ .flowers_visited = 17 } }, 102 102 Insect{ .grasshopper = Grasshopper{ .distance_hopped = 32 } },
+2 -2
exercises/096_memory_allocation.zig
··· 52 52 53 53 pub fn main() !void { 54 54 // pretend this was defined by reading in user input 55 - var arr: []const f64 = &[_]f64{ 0.3, 0.2, 0.1, 0.1, 0.4 }; 55 + const arr: []const f64 = &[_]f64{ 0.3, 0.2, 0.1, 0.1, 0.4 }; 56 56 57 57 // initialize the allocator 58 58 var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); ··· 64 64 const allocator = arena.allocator(); 65 65 66 66 // allocate memory for this array 67 - var avg: []f64 = ???; 67 + const avg: []f64 = ???; 68 68 69 69 runningAverage(arr, avg); 70 70 std.debug.print("Running Average: ", .{});
+4 -4
patches/patches/025_errors5.patch
··· 1 - --- exercises/025_errors5.zig 2023-10-03 22:15:22.122241138 +0200 2 - +++ answers/025_errors5.zig 2023-10-05 20:04:06.952764946 +0200 1 + --- exercises/025_errors5.zig 2023-11-21 14:22:48.159250165 +0100 2 + +++ answers/025_errors5.zig 2023-11-21 14:25:01.338277886 +0100 3 3 @@ -26,7 +26,7 @@ 4 4 // This function needs to return any error which might come back from detect(). 5 5 // Please use a "try" statement rather than a "catch". 6 6 // 7 - - var x = detect(n); 8 - + var x = try detect(n); 7 + - const x = detect(n); 8 + + const x = try detect(n); 9 9 10 10 return x + 5; 11 11 }
+2 -2
patches/patches/075_quiz8.patch
··· 1 - --- exercises/075_quiz8.zig 2023-10-03 22:15:22.125574535 +0200 2 - +++ answers/075_quiz8.zig 2023-10-05 20:04:07.182769252 +0200 1 + --- exercises/075_quiz8.zig 2023-11-21 14:48:15.440702720 +0100 2 + +++ answers/075_quiz8.zig 2023-11-21 14:50:23.453311616 +0100 3 3 @@ -49,7 +49,11 @@ 4 4 // 5 5 // Please fill in the body of this function!
+6 -6
patches/patches/080_anonymous_structs.patch
··· 1 - --- exercises/080_anonymous_structs.zig 2023-10-03 22:15:22.125574535 +0200 2 - +++ answers/080_anonymous_structs.zig 2023-10-05 20:04:07.202769626 +0200 1 + --- exercises/080_anonymous_structs.zig 2023-11-21 14:52:54.312749682 +0100 2 + +++ answers/080_anonymous_structs.zig 2023-11-21 14:52:43.909225238 +0100 3 3 @@ -48,13 +48,13 @@ 4 4 // * circle1 should hold i32 integers 5 5 // * circle2 should hold f32 floats 6 6 // 7 - - var circle1 = ??? { 8 - + var circle1 = Circle(i32){ 7 + - const circle1 = ??? { 8 + + const circle1 = Circle(i32){ 9 9 .center_x = 25, 10 10 .center_y = 70, 11 11 .radius = 15, 12 12 }; 13 13 14 - - var circle2 = ??? { 15 - + var circle2 = Circle(f32){ 14 + - const circle2 = ??? { 15 + + const circle2 = Circle(f32){ 16 16 .center_x = 25.234, 17 17 .center_y = 70.999, 18 18 .radius = 15.714,
+4 -4
patches/patches/096_memory_allocation.patch
··· 1 - --- exercises/096_memory_allocation.zig 2023-10-03 22:15:22.125574535 +0200 2 - +++ answers/096_memory_allocation.zig 2023-10-05 20:04:07.276104333 +0200 1 + --- exercises/096_memory_allocation.zig 2023-11-21 14:55:33.805678390 +0100 2 + +++ answers/096_memory_allocation.zig 2023-11-21 14:56:00.236163484 +0100 3 3 @@ -64,7 +64,7 @@ 4 4 const allocator = arena.allocator(); 5 5 6 6 // allocate memory for this array 7 - - var avg: []f64 = ???; 8 - + var avg: []f64 = try allocator.alloc(f64, arr.len); 7 + - const avg: []f64 = ???; 8 + + const avg: []f64 = try allocator.alloc(f64, arr.len); 9 9 10 10 runningAverage(arr, avg); 11 11 std.debug.print("Running Average: ", .{});