Skip to content

Commit 371beb1

Browse files
authored
Merge pull request #329 from lorrding/refactor-var-to-const
Change `var` to `const` in some exercises
2 parents 08e8a0f + b3a254f commit 371beb1

18 files changed

+33
-33
lines changed

exercises/010_if2.zig

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
//
22
// If statements are also valid expressions:
33
//
4-
// var foo: u8 = if (a) 2 else 3;
4+
// const foo: u8 = if (a) 2 else 3;
55
//
66
const std = @import("std");
77

88
pub fn main() void {
9-
var discount = true;
9+
const discount = true;
1010

1111
// Please use an if...else expression to set "price".
1212
// If discount is true, the price should be $17, otherwise $20:
13-
var price: u8 = if ???;
13+
const price: u8 = if ???;
1414

1515
std.debug.print("With the discount, the price is ${}.\n", .{price});
1616
}

exercises/016_for2.zig

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

exercises/017_quiz2.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const std = import standard library;
1313

1414
function main() void {
1515
var i: u8 = 1;
16-
var stop_at: u8 = 16;
16+
const stop_at: u8 = 16;
1717

1818
// What kind of loop is this? A 'for' or a 'while'?
1919
??? (i <= stop_at) : (i += 1) {

exercises/023_errors3.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const std = @import("std");
1111
const MyNumberError = error{TooSmall};
1212

1313
pub fn main() void {
14-
var a: u32 = addTwenty(44) catch 22;
15-
var b: u32 = addTwenty(4) ??? 22;
14+
const a: u32 = addTwenty(44) catch 22;
15+
const b: u32 = addTwenty(4) ??? 22;
1616

1717
std.debug.print("a={}, b={}\n", .{ a, b });
1818
}

exercises/024_errors4.zig

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ const MyNumberError = error{
2121
pub fn main() void {
2222
// The "catch 0" below is a temporary hack to deal with
2323
// makeJustRight()'s returned error union (for now).
24-
var a: u32 = makeJustRight(44) catch 0;
25-
var b: u32 = makeJustRight(14) catch 0;
26-
var c: u32 = makeJustRight(4) catch 0;
24+
const a: u32 = makeJustRight(44) catch 0;
25+
const b: u32 = makeJustRight(14) catch 0;
26+
const c: u32 = makeJustRight(4) catch 0;
2727

2828
std.debug.print("a={}, b={}, c={}\n", .{ a, b, c });
2929
}

exercises/025_errors5.zig

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ const MyNumberError = error{
1515
};
1616

1717
pub fn main() void {
18-
var a: u32 = addFive(44) catch 0;
19-
var b: u32 = addFive(14) catch 0;
20-
var c: u32 = addFive(4) catch 0;
18+
const a: u32 = addFive(44) catch 0;
19+
const b: u32 = addFive(14) catch 0;
20+
const c: u32 = addFive(4) catch 0;
2121

2222
std.debug.print("a={}, b={}, c={}\n", .{ a, b, c });
2323
}

exercises/031_switch2.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// What's really nice is that you can use a switch statement as an
33
// expression to return a value.
44
//
5-
// var a = switch (x) {
5+
// const a = switch (x) {
66
// 1 => 9,
77
// 2 => 16,
88
// 3 => 7,

exercises/036_enums2.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
// const Stuff = enum(u8){ foo = 16 };
77
//
88
// You can get the integer out with a builtin function,
9-
// @enumToInt(). We'll learn about builtins properly in a later
9+
// @intFromEnum(). We'll learn about builtins properly in a later
1010
// exercise.
1111
//
12-
// var my_stuff: u8 = @enumToInt(Stuff.foo);
12+
// const my_stuff: u8 = @intFromEnum(Stuff.foo);
1313
//
1414
// Note how that built-in function starts with "@" just like the
1515
// @import() function we've been using.

exercises/045_optionals.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn main() void {
2929

3030
// Please threaten the result so that answer is either the
3131
// integer value from deepThought() OR the number 42:
32-
var answer: u8 = result;
32+
const answer: u8 = result;
3333

3434
std.debug.print("The Ultimate Answer: {}.\n", .{answer});
3535
}

exercises/047_methods.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub fn main() void {
7878
};
7979

8080
var aliens_alive = aliens.len;
81-
var heat_ray = HeatRay{ .damage = 7 }; // We've been given a heat ray weapon.
81+
const heat_ray = HeatRay{ .damage = 7 }; // We've been given a heat ray weapon.
8282

8383
// We'll keep checking to see if we've killed all the aliens yet.
8484
while (aliens_alive > 0) {

exercises/059_integers.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
const print = @import("std").debug.print;
1919

2020
pub fn main() void {
21-
var zig = [_]u8{
21+
const zig = [_]u8{
2222
0o131, // octal
2323
0b1101000, // binary
2424
0x66, // hex

exercises/060_floats.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn main() void {
4343
//
4444
// We'll convert this weight from tons to kilograms at a
4545
// conversion of 907.18kg to the ton.
46-
var shuttle_weight: f16 = 907.18 * 2200;
46+
const shuttle_weight: f16 = 907.18 * 2200;
4747

4848
// By default, float values are formatted in scientific
4949
// notation. Try experimenting with '{d}' and '{d:.3}' to see

exercises/064_builtins.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Ziglings exercise.
55
//
66
// We've also seen @intCast() in "016_for2.zig", "058_quiz7.zig";
7-
// and @enumToInt() in "036_enums2.zig".
7+
// and @intFromEnum() in "036_enums2.zig".
88
//
99
// Builtins are special because they are intrinsic to the Zig
1010
// language itself (as opposed to being provided in the standard

exercises/065_builtins2.zig

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//
22
// Zig has builtins for mathematical operations such as...
33
//
4-
// @sqrt @sin @cos
5-
// @exp @log @floor
4+
// @sqrt @sin @cos
5+
// @exp @log @floor
66
//
77
// ...and lots of type casting operations such as...
88
//
9-
// @as @intToError @intToFloat
10-
// @intToPtr @ptrToInt @enumToInt
9+
// @as @errorFromInt @floatFromInt
10+
// @ptrFromInt @intFromPtr @intFromEnum
1111
//
1212
// Spending part of a rainy day skimming through the complete
1313
// list of builtins in the official Zig documentation wouldn't be

patches/patches/010_if2.patch

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
13c13
2-
< var price: u8 = if ???;
2+
< const price: u8 = if ???;
33
---
4-
> var price: u8 = if (discount) 17 else 20;
4+
> const price: u8 = if (discount) 17 else 20;

patches/patches/023_errors3.patch

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
15c15
2-
< var b: u32 = addTwenty(4) ??? 22;
2+
< const b: u32 = addTwenty(4) ??? 22;
33
---
4-
> var b: u32 = addTwenty(4) catch 22;
4+
> const b: u32 = addTwenty(4) catch 22;
55
22c22
66
< fn addTwenty(n: u32) ??? {
77
---

patches/patches/045_optionals.patch

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
32c32
2-
< var answer: u8 = result;
2+
< const answer: u8 = result;
33
---
4-
> var answer: u8 = result orelse 42;
4+
> const answer: u8 = result orelse 42;

patches/patches/060_floats.patch

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
43c43
2-
< var shuttle_weight: f16 = 907.18 * 2200;
2+
< const shuttle_weight: f16 = 907.18 * 2200;
33
---
4-
> var shuttle_weight: f32 = 907.18 * 2200.0;
4+
> const shuttle_weight: f32 = 907.18 * 2200.0;

0 commit comments

Comments
 (0)