Converted var to const if there is no mutation in var.

This is checked from compiler version 0.12.0-dev.1664
This commit is contained in:
Chris Boesch 2023-11-21 15:01:22 +01:00
parent b7015c2d9d
commit 7679f93f68
21 changed files with 46 additions and 46 deletions

View file

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

View file

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

View file

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

View file

@ -24,7 +24,7 @@ const Elephant = struct {
pub fn print(self: *Elephant) void { pub fn print(self: *Elephant) void {
// Prints elephant letter and [v]isited // 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 }); std.debug.print("{u}{u} ", .{ self.letter, v });
} }
}; };

View file

@ -37,7 +37,7 @@ const Elephant = struct {
pub fn print(self: *Elephant) void { pub fn print(self: *Elephant) void {
// Prints elephant letter and [v]isited // 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 }); std.debug.print("{u}{u} ", .{ self.letter, v });
} }
}; };

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -39,7 +39,7 @@ pub fn main() void {
// This gets the digit from the "instruction". Can you // This gets the digit from the "instruction". Can you
// figure out why we subtract '0' from it? // 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 // This 'switch' statement contains the actual work done
// at runtime. At first, this doesn't seem exciting... // at runtime. At first, this doesn't seem exciting...

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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