Merge pull request #329 from lorrding/refactor-var-to-const

Change `var` to `const` in some exercises
This commit is contained in:
Chris Boesch 2023-06-22 16:51:03 +02:00 committed by GitHub
commit 2705e16c36
18 changed files with 33 additions and 33 deletions

View file

@ -1,16 +1,16 @@
//
// If statements are also valid expressions:
//
// var foo: u8 = if (a) 2 else 3;
// const foo: u8 = if (a) 2 else 3;
//
const std = @import("std");
pub fn main() void {
var discount = true;
const discount = true;
// Please use an if...else expression to set "price".
// If discount is true, the price should be $17, otherwise $20:
var price: u8 = if ???;
const price: u8 = if ???;
std.debug.print("With the discount, the price is ${}.\n", .{price});
}

View file

@ -29,7 +29,7 @@ pub fn main() void {
// Note that we convert the usize i to a u32 with
// @intCast(), a builtin function just like @import().
// We'll learn about these properly in a later exercise.
var place_value = std.math.pow(u32, 2, @intCast(u32, i));
const place_value = std.math.pow(u32, 2, @intCast(u32, i));
value += place_value * bit;
}

View file

@ -13,7 +13,7 @@ const std = import standard library;
function main() void {
var i: u8 = 1;
var stop_at: u8 = 16;
const stop_at: u8 = 16;
// What kind of loop is this? A 'for' or a 'while'?
??? (i <= stop_at) : (i += 1) {

View file

@ -11,8 +11,8 @@ const std = @import("std");
const MyNumberError = error{TooSmall};
pub fn main() void {
var a: u32 = addTwenty(44) catch 22;
var b: u32 = addTwenty(4) ??? 22;
const a: u32 = addTwenty(44) catch 22;
const b: u32 = addTwenty(4) ??? 22;
std.debug.print("a={}, b={}\n", .{ a, b });
}

View file

@ -21,9 +21,9 @@ const MyNumberError = error{
pub fn main() void {
// The "catch 0" below is a temporary hack to deal with
// makeJustRight()'s returned error union (for now).
var a: u32 = makeJustRight(44) catch 0;
var b: u32 = makeJustRight(14) catch 0;
var c: u32 = makeJustRight(4) catch 0;
const a: u32 = makeJustRight(44) catch 0;
const b: u32 = makeJustRight(14) catch 0;
const c: u32 = makeJustRight(4) catch 0;
std.debug.print("a={}, b={}, c={}\n", .{ a, b, c });
}

View file

@ -15,9 +15,9 @@ const MyNumberError = error{
};
pub fn main() void {
var a: u32 = addFive(44) catch 0;
var b: u32 = addFive(14) catch 0;
var c: u32 = addFive(4) catch 0;
const a: u32 = addFive(44) catch 0;
const b: u32 = addFive(14) catch 0;
const c: u32 = addFive(4) catch 0;
std.debug.print("a={}, b={}, c={}\n", .{ a, b, c });
}

View file

@ -2,7 +2,7 @@
// What's really nice is that you can use a switch statement as an
// expression to return a value.
//
// var a = switch (x) {
// const a = switch (x) {
// 1 => 9,
// 2 => 16,
// 3 => 7,

View file

@ -6,10 +6,10 @@
// const Stuff = enum(u8){ foo = 16 };
//
// You can get the integer out with a builtin function,
// @enumToInt(). We'll learn about builtins properly in a later
// @intFromEnum(). We'll learn about builtins properly in a later
// exercise.
//
// var my_stuff: u8 = @enumToInt(Stuff.foo);
// const my_stuff: u8 = @intFromEnum(Stuff.foo);
//
// Note how that built-in function starts with "@" just like the
// @import() function we've been using.

View file

@ -29,7 +29,7 @@ pub fn main() void {
// Please threaten the result so that answer is either the
// integer value from deepThought() OR the number 42:
var answer: u8 = result;
const answer: u8 = result;
std.debug.print("The Ultimate Answer: {}.\n", .{answer});
}

View file

@ -78,7 +78,7 @@ pub fn main() void {
};
var aliens_alive = aliens.len;
var heat_ray = HeatRay{ .damage = 7 }; // We've been given a heat ray weapon.
const heat_ray = HeatRay{ .damage = 7 }; // We've been given a heat ray weapon.
// We'll keep checking to see if we've killed all the aliens yet.
while (aliens_alive > 0) {

View file

@ -18,7 +18,7 @@
const print = @import("std").debug.print;
pub fn main() void {
var zig = [_]u8{
const zig = [_]u8{
0o131, // octal
0b1101000, // binary
0x66, // hex

View file

@ -43,7 +43,7 @@ pub fn main() void {
//
// We'll convert this weight from tons to kilograms at a
// conversion of 907.18kg to the ton.
var shuttle_weight: f16 = 907.18 * 2200;
const shuttle_weight: f16 = 907.18 * 2200;
// By default, float values are formatted in scientific
// notation. Try experimenting with '{d}' and '{d:.3}' to see

View file

@ -4,7 +4,7 @@
// Ziglings exercise.
//
// We've also seen @intCast() in "016_for2.zig", "058_quiz7.zig";
// and @enumToInt() in "036_enums2.zig".
// and @intFromEnum() in "036_enums2.zig".
//
// Builtins are special because they are intrinsic to the Zig
// language itself (as opposed to being provided in the standard

View file

@ -6,8 +6,8 @@
//
// ...and lots of type casting operations such as...
//
// @as @intToError @intToFloat
// @intToPtr @ptrToInt @enumToInt
// @as @errorFromInt @floatFromInt
// @ptrFromInt @intFromPtr @intFromEnum
//
// Spending part of a rainy day skimming through the complete
// list of builtins in the official Zig documentation wouldn't be

View file

@ -1,4 +1,4 @@
13c13
< var price: u8 = if ???;
< const price: u8 = if ???;
---
> var price: u8 = if (discount) 17 else 20;
> const price: u8 = if (discount) 17 else 20;

View file

@ -1,7 +1,7 @@
15c15
< var b: u32 = addTwenty(4) ??? 22;
< const b: u32 = addTwenty(4) ??? 22;
---
> var b: u32 = addTwenty(4) catch 22;
> const b: u32 = addTwenty(4) catch 22;
22c22
< fn addTwenty(n: u32) ??? {
---

View file

@ -1,4 +1,4 @@
32c32
< var answer: u8 = result;
< const answer: u8 = result;
---
> var answer: u8 = result orelse 42;
> const answer: u8 = result orelse 42;

View file

@ -1,4 +1,4 @@
43c43
< var shuttle_weight: f16 = 907.18 * 2200;
< const shuttle_weight: f16 = 907.18 * 2200;
---
> var shuttle_weight: f32 = 907.18 * 2200.0;
> const shuttle_weight: f32 = 907.18 * 2200.0;