Browse Source

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

Chris Boesch 1 year ago
parent
commit
7679f93f68

+ 1 - 1
exercises/025_errors5.zig

@@ -26,7 +26,7 @@ fn addFive(n: u32) MyNumberError!u32 {
     // This function needs to return any error which might come back from detect().
     // Please use a "try" statement rather than a "catch".
     //
-    var x = detect(n);
+    const x = detect(n);
 
     return x + 5;
 }

+ 2 - 2
exercises/029_errdefer.zig

@@ -21,8 +21,8 @@ const MyErr = error{ GetFail, IncFail };
 
 pub fn main() void {
     // We simply quit the entire program if we fail to get a number:
-    var a: u32 = makeNumber() catch return;
-    var b: u32 = makeNumber() catch return;
+    const a: u32 = makeNumber() catch return;
+    const b: u32 = makeNumber() catch return;
 
     std.debug.print("Numbers: {}, {}\n", .{ a, b });
 }

+ 1 - 1
exercises/039_pointers.zig

@@ -24,7 +24,7 @@ const std = @import("std");
 
 pub fn main() void {
     var num1: u8 = 5;
-    var num1_pointer: *u8 = &num1;
+    const num1_pointer: *u8 = &num1;
 
     var num2: u8 = undefined;
 

+ 1 - 1
exercises/048_methods2.zig

@@ -24,7 +24,7 @@ const Elephant = struct {
 
     pub fn print(self: *Elephant) void {
         // Prints elephant letter and [v]isited
-        var v: u8 = if (self.visited) 'v' else ' ';
+        const v: u8 = if (self.visited) 'v' else ' ';
         std.debug.print("{u}{u} ", .{ self.letter, v });
     }
 };

+ 1 - 1
exercises/049_quiz6.zig

@@ -37,7 +37,7 @@ const Elephant = struct {
 
     pub fn print(self: *Elephant) void {
         // Prints elephant letter and [v]isited
-        var v: u8 = if (self.visited) 'v' else ' ';
+        const v: u8 = if (self.visited) 'v' else ' ';
         std.debug.print("{u}{u} ", .{ self.letter, v });
     }
 };

+ 2 - 2
exercises/055_unions.zig

@@ -53,8 +53,8 @@ const AntOrBee = enum { a, b };
 
 pub fn main() void {
     // We'll just make one bee and one ant to test them out:
-    var ant = Insect{ .still_alive = true };
-    var bee = Insect{ .flowers_visited = 15 };
+    const ant = Insect{ .still_alive = true };
+    const bee = Insect{ .flowers_visited = 15 };
 
     std.debug.print("Insect report! ", .{});
 

+ 2 - 2
exercises/056_unions2.zig

@@ -38,8 +38,8 @@ const Insect = union(InsectStat) {
 };
 
 pub fn main() void {
-    var ant = Insect{ .still_alive = true };
-    var bee = Insect{ .flowers_visited = 16 };
+    const ant = Insect{ .still_alive = true };
+    const bee = Insect{ .flowers_visited = 16 };
 
     std.debug.print("Insect report! ", .{});
 

+ 2 - 2
exercises/057_unions3.zig

@@ -21,8 +21,8 @@ const Insect = union(InsectStat) {
 };
 
 pub fn main() void {
-    var ant = Insect{ .still_alive = true };
-    var bee = Insect{ .flowers_visited = 17 };
+    const ant = Insect{ .still_alive = true };
+    const bee = Insect{ .flowers_visited = 17 };
 
     std.debug.print("Insect report! ", .{});
 

+ 2 - 2
exercises/058_quiz7.zig

@@ -273,7 +273,7 @@ const HermitsNotebook = struct {
     // distance) than the one we'd noted before. If it is, we
     // overwrite the old entry with the new one.
     fn checkNote(self: *HermitsNotebook, note: NotebookEntry) void {
-        var existing_entry = self.getEntry(note.place);
+        const existing_entry = self.getEntry(note.place);
 
         if (existing_entry == null) {
             self.entries[self.end_of_entries] = note;
@@ -386,7 +386,7 @@ pub fn main() void {
     // "start" entry we just added) until we run out, at which point
     // we'll have checked every reachable Place.
     while (notebook.hasNextEntry()) {
-        var place_entry = notebook.getNextEntry();
+        const place_entry = notebook.getNextEntry();
 
         // For every Path that leads FROM the current Place, create a
         // new note (in the form of a NotebookEntry) with the

+ 4 - 4
exercises/067_comptime2.zig

@@ -38,16 +38,16 @@ pub fn main() void {
     var count = 0;
 
     count += 1;
-    var a1: [count]u8 = .{'A'} ** count;
+    const a1: [count]u8 = .{'A'} ** count;
 
     count += 1;
-    var a2: [count]u8 = .{'B'} ** count;
+    const a2: [count]u8 = .{'B'} ** count;
 
     count += 1;
-    var a3: [count]u8 = .{'C'} ** count;
+    const a3: [count]u8 = .{'C'} ** count;
 
     count += 1;
-    var a4: [count]u8 = .{'D'} ** count;
+    const a4: [count]u8 = .{'D'} ** count;
 
     print("{s} {s} {s} {s}\n", .{ a1, a2, a3, a4 });
 

+ 3 - 3
exercises/070_comptime5.zig

@@ -83,19 +83,19 @@ const DuctError = error{UnmatchedDiameters};
 
 pub fn main() void {
     // This is a real duck!
-    var ducky1 = Duck{
+    const ducky1 = Duck{
         .eggs = 0,
         .loudness = 3,
     };
 
     // This is not a real duck, but it has quack() and waddle()
     // abilities, so it's still a "duck".
-    var ducky2 = RubberDuck{
+    const ducky2 = RubberDuck{
         .in_bath = false,
     };
 
     // This is not even remotely a duck.
-    var ducky3 = Duct{
+    const ducky3 = Duct{
         .diameter = 17,
         .length = 165,
         .galvanized = true,

+ 1 - 1
exercises/072_comptime7.zig

@@ -39,7 +39,7 @@ pub fn main() void {
 
         // This gets the digit from the "instruction". Can you
         // figure out why we subtract '0' from it?
-        comptime var digit = instructions[i + 1] - '0';
+        const digit = instructions[i + 1] - '0';
 
         // This 'switch' statement contains the actual work done
         // at runtime. At first, this doesn't seem exciting...

+ 2 - 2
exercises/075_quiz8.zig

@@ -110,7 +110,7 @@ const HermitsNotebook = struct {
     }
 
     fn checkNote(self: *HermitsNotebook, note: NotebookEntry) void {
-        var existing_entry = self.getEntry(note.place);
+        const existing_entry = self.getEntry(note.place);
 
         if (existing_entry == null) {
             self.entries[self.end_of_entries] = note;
@@ -180,7 +180,7 @@ pub fn main() void {
     notebook.checkNote(working_note);
 
     while (notebook.hasNextEntry()) {
-        var place_entry = notebook.getNextEntry();
+        const place_entry = notebook.getNextEntry();
 
         for (place_entry.place.paths) |*path| {
             working_note = NotebookEntry{

+ 1 - 1
exercises/076_sentinels.zig

@@ -46,7 +46,7 @@ pub fn main() void {
     var nums = [_:0]u32{ 1, 2, 3, 4, 5, 6 };
 
     // And here's a zero-terminated many-item pointer:
-    var ptr: [*:0]u32 = &nums;
+    const ptr: [*:0]u32 = &nums;
 
     // For fun, let's replace the value at position 3 with the
     // sentinel value 0. This seems kind of naughty.

+ 2 - 2
exercises/080_anonymous_structs.zig

@@ -48,13 +48,13 @@ pub fn main() void {
     // * circle1 should hold i32 integers
     // * circle2 should hold f32 floats
     //
-    var circle1 = ??? {
+    const circle1 = ??? {
         .center_x = 25,
         .center_y = 70,
         .radius = 15,
     };
 
-    var circle2 = ??? {
+    const circle2 = ??? {
         .center_x = 25.234,
         .center_y = 70.999,
         .radius = 15.714,

+ 1 - 1
exercises/092_interfaces.zig

@@ -96,7 +96,7 @@ const Insect = union(enum) {
 };
 
 pub fn main() !void {
-    var my_insects = [_]Insect{
+    const my_insects = [_]Insect{
         Insect{ .ant = Ant{ .still_alive = true } },
         Insect{ .bee = Bee{ .flowers_visited = 17 } },
         Insect{ .grasshopper = Grasshopper{ .distance_hopped = 32 } },

+ 2 - 2
exercises/096_memory_allocation.zig

@@ -52,7 +52,7 @@ fn runningAverage(arr: []const f64, avg: []f64) void {
 
 pub fn main() !void {
     // pretend this was defined by reading in user input
-    var arr: []const f64 = &[_]f64{ 0.3, 0.2, 0.1, 0.1, 0.4 };
+    const arr: []const f64 = &[_]f64{ 0.3, 0.2, 0.1, 0.1, 0.4 };
 
     // initialize the allocator
     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
@@ -64,7 +64,7 @@ pub fn main() !void {
     const allocator = arena.allocator();
 
     // allocate memory for this array
-    var avg: []f64 = ???;
+    const avg: []f64 = ???;
 
     runningAverage(arr, avg);
     std.debug.print("Running Average: ", .{});

+ 4 - 4
patches/patches/025_errors5.patch

@@ -1,11 +1,11 @@
---- exercises/025_errors5.zig	2023-10-03 22:15:22.122241138 +0200
-+++ answers/025_errors5.zig	2023-10-05 20:04:06.952764946 +0200
+--- exercises/025_errors5.zig	2023-11-21 14:22:48.159250165 +0100
++++ answers/025_errors5.zig	2023-11-21 14:25:01.338277886 +0100
 @@ -26,7 +26,7 @@
      // This function needs to return any error which might come back from detect().
      // Please use a "try" statement rather than a "catch".
      //
--    var x = detect(n);
-+    var x = try detect(n);
+-    const x = detect(n);
++    const x = try detect(n);
  
      return x + 5;
  }

+ 2 - 2
patches/patches/075_quiz8.patch

@@ -1,5 +1,5 @@
---- exercises/075_quiz8.zig	2023-10-03 22:15:22.125574535 +0200
-+++ answers/075_quiz8.zig	2023-10-05 20:04:07.182769252 +0200
+--- exercises/075_quiz8.zig	2023-11-21 14:48:15.440702720 +0100
++++ answers/075_quiz8.zig	2023-11-21 14:50:23.453311616 +0100
 @@ -49,7 +49,11 @@
  //
  // Please fill in the body of this function!

+ 6 - 6
patches/patches/080_anonymous_structs.patch

@@ -1,18 +1,18 @@
---- exercises/080_anonymous_structs.zig	2023-10-03 22:15:22.125574535 +0200
-+++ answers/080_anonymous_structs.zig	2023-10-05 20:04:07.202769626 +0200
+--- exercises/080_anonymous_structs.zig	2023-11-21 14:52:54.312749682 +0100
++++ answers/080_anonymous_structs.zig	2023-11-21 14:52:43.909225238 +0100
 @@ -48,13 +48,13 @@
      // * circle1 should hold i32 integers
      // * circle2 should hold f32 floats
      //
--    var circle1 = ??? {
-+    var circle1 = Circle(i32){
+-    const circle1 = ??? {
++    const circle1 = Circle(i32){
          .center_x = 25,
          .center_y = 70,
          .radius = 15,
      };
  
--    var circle2 = ??? {
-+    var circle2 = Circle(f32){
+-    const circle2 = ??? {
++    const circle2 = Circle(f32){
          .center_x = 25.234,
          .center_y = 70.999,
          .radius = 15.714,

+ 4 - 4
patches/patches/096_memory_allocation.patch

@@ -1,11 +1,11 @@
---- exercises/096_memory_allocation.zig	2023-10-03 22:15:22.125574535 +0200
-+++ answers/096_memory_allocation.zig	2023-10-05 20:04:07.276104333 +0200
+--- exercises/096_memory_allocation.zig	2023-11-21 14:55:33.805678390 +0100
++++ answers/096_memory_allocation.zig	2023-11-21 14:56:00.236163484 +0100
 @@ -64,7 +64,7 @@
      const allocator = arena.allocator();
  
      // allocate memory for this array
--    var avg: []f64 = ???;
-+    var avg: []f64 = try allocator.alloc(f64, arr.len);
+-    const avg: []f64 = ???;
++    const avg: []f64 = try allocator.alloc(f64, arr.len);
  
      runningAverage(arr, avg);
      std.debug.print("Running Average: ", .{});