my ziglings
0

Configure Feed

Select the types of activity you want to include in your feed.

var to const when posssible

lording (Jun 22, 2023, 9:41 AM UTC) d2d3dfa2 f09a87c3

+27 -27
+3 -3
exercises/010_if2.zig
··· 1 1 // 2 2 // If statements are also valid expressions: 3 3 // 4 - // var foo: u8 = if (a) 2 else 3; 4 + // const foo: u8 = if (a) 2 else 3; 5 5 // 6 6 const std = @import("std"); 7 7 8 8 pub fn main() void { 9 - var discount = true; 9 + const discount = true; 10 10 11 11 // Please use an if...else expression to set "price". 12 12 // If discount is true, the price should be $17, otherwise $20: 13 - var price: u8 = if ???; 13 + const price: u8 = if ???; 14 14 15 15 std.debug.print("With the discount, the price is ${}.\n", .{price}); 16 16 }
+1 -1
exercises/016_for2.zig
··· 29 29 // Note that we convert the usize i to a u32 with 30 30 // @intCast(), a builtin function just like @import(). 31 31 // 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)); 33 33 value += place_value * bit; 34 34 } 35 35
+1 -1
exercises/017_quiz2.zig
··· 13 13 14 14 function main() void { 15 15 var i: u8 = 1; 16 - var stop_at: u8 = 16; 16 + const stop_at: u8 = 16; 17 17 18 18 // What kind of loop is this? A 'for' or a 'while'? 19 19 ??? (i <= stop_at) : (i += 1) {
+2 -2
exercises/023_errors3.zig
··· 11 11 const MyNumberError = error{TooSmall}; 12 12 13 13 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; 16 16 17 17 std.debug.print("a={}, b={}\n", .{ a, b }); 18 18 }
+3 -3
exercises/024_errors4.zig
··· 21 21 pub fn main() void { 22 22 // The "catch 0" below is a temporary hack to deal with 23 23 // 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; 27 27 28 28 std.debug.print("a={}, b={}, c={}\n", .{ a, b, c }); 29 29 }
+3 -3
exercises/025_errors5.zig
··· 15 15 }; 16 16 17 17 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; 21 21 22 22 std.debug.print("a={}, b={}, c={}\n", .{ a, b, c }); 23 23 }
+1 -1
exercises/031_switch2.zig
··· 2 2 // What's really nice is that you can use a switch statement as an 3 3 // expression to return a value. 4 4 // 5 - // var a = switch (x) { 5 + // const a = switch (x) { 6 6 // 1 => 9, 7 7 // 2 => 16, 8 8 // 3 => 7,
+1 -1
exercises/036_enums2.zig
··· 9 9 // @enumToInt(). We'll learn about builtins properly in a later 10 10 // exercise. 11 11 // 12 - // var my_stuff: u8 = @enumToInt(Stuff.foo); 12 + // const my_stuff: u8 = @enumToInt(Stuff.foo); 13 13 // 14 14 // Note how that built-in function starts with "@" just like the 15 15 // @import() function we've been using.
+1 -1
exercises/045_optionals.zig
··· 29 29 30 30 // Please threaten the result so that answer is either the 31 31 // integer value from deepThought() OR the number 42: 32 - var answer: u8 = result; 32 + const answer: u8 = result; 33 33 34 34 std.debug.print("The Ultimate Answer: {}.\n", .{answer}); 35 35 }
+1 -1
exercises/047_methods.zig
··· 78 78 }; 79 79 80 80 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. 82 82 83 83 // We'll keep checking to see if we've killed all the aliens yet. 84 84 while (aliens_alive > 0) {
+1 -1
exercises/059_integers.zig
··· 18 18 const print = @import("std").debug.print; 19 19 20 20 pub fn main() void { 21 - var zig = [_]u8{ 21 + const zig = [_]u8{ 22 22 0o131, // octal 23 23 0b1101000, // binary 24 24 0x66, // hex
+1 -1
exercises/060_floats.zig
··· 43 43 // 44 44 // We'll convert this weight from tons to kilograms at a 45 45 // conversion of 907.18kg to the ton. 46 - var shuttle_weight: f16 = 907.18 * 2200; 46 + const shuttle_weight: f16 = 907.18 * 2200; 47 47 48 48 // By default, float values are formatted in scientific 49 49 // notation. Try experimenting with '{d}' and '{d:.3}' to see
+2 -2
patches/patches/010_if2.patch
··· 1 1 13c13 2 - < var price: u8 = if ???; 2 + < const price: u8 = if ???; 3 3 --- 4 - > var price: u8 = if (discount) 17 else 20; 4 + > const price: u8 = if (discount) 17 else 20;
+2 -2
patches/patches/023_errors3.patch
··· 1 1 15c15 2 - < var b: u32 = addTwenty(4) ??? 22; 2 + < const b: u32 = addTwenty(4) ??? 22; 3 3 --- 4 - > var b: u32 = addTwenty(4) catch 22; 4 + > const b: u32 = addTwenty(4) catch 22; 5 5 22c22 6 6 < fn addTwenty(n: u32) ??? { 7 7 ---
+2 -2
patches/patches/045_optionals.patch
··· 1 1 32c32 2 - < var answer: u8 = result; 2 + < const answer: u8 = result; 3 3 --- 4 - > var answer: u8 = result orelse 42; 4 + > const answer: u8 = result orelse 42;
+2 -2
patches/patches/060_floats.patch
··· 1 1 43c43 2 - < var shuttle_weight: f16 = 907.18 * 2200; 2 + < const shuttle_weight: f16 = 907.18 * 2200; 3 3 --- 4 - > var shuttle_weight: f32 = 907.18 * 2200.0; 4 + > const shuttle_weight: f32 = 907.18 * 2200.0;