mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-11-09 11:40:46 -05:00
Merge pull request 'Converted var to const if there is no mutation in the var.' (#28) from var_const into main
Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/28
This commit is contained in:
commit
f987a06ccc
22 changed files with 49 additions and 49 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 });
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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 });
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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 });
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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! ", .{});
|
||||
|
||||
|
|
|
@ -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! ", .{});
|
||||
|
||||
|
|
|
@ -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! ", .{});
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 });
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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...
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 } },
|
||||
|
|
|
@ -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: ", .{});
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
--- exercises/067_comptime2.zig 2023-10-03 22:15:22.125574535 +0200
|
||||
+++ answers/067_comptime2.zig 2023-10-05 20:04:07.146101899 +0200
|
||||
--- exercises/067_comptime2.zig 2023-11-21 14:36:12.080295365 +0100
|
||||
+++ answers/067_comptime2.zig 2023-11-21 15:11:50.814098876 +0100
|
||||
@@ -35,7 +35,7 @@
|
||||
// In this contrived example, we've decided to allocate some
|
||||
// arrays using a variable count! But something's missing...
|
||||
|
@ -8,4 +8,4 @@
|
|||
+ comptime var count = 0;
|
||||
|
||||
count += 1;
|
||||
var a1: [count]u8 = .{'A'} ** count;
|
||||
const a1: [count]u8 = .{'A'} ** count;
|
||||
|
|
|
@ -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!
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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: ", .{});
|
||||
|
|
Loading…
Reference in a new issue