var to const when posssible

This commit is contained in:
lording 2023-06-22 09:41:41 +00:00
parent f09a87c348
commit d2d3dfa277
16 changed files with 27 additions and 27 deletions

View file

@ -1,16 +1,16 @@
// //
// If statements are also valid expressions: // 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"); const std = @import("std");
pub fn main() void { pub fn main() void {
var discount = true; const discount = true;
// Please use an if...else expression to set "price". // Please use an if...else expression to set "price".
// If discount is true, the price should be $17, otherwise $20: // 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}); 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 // Note that we convert the usize i to a u32 with
// @intCast(), a builtin function just like @import(). // @intCast(), a builtin function just like @import().
// We'll learn about these properly in a later exercise. // 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; value += place_value * bit;
} }

View file

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

View file

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

View file

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

View file

@ -15,9 +15,9 @@ const MyNumberError = error{
}; };
pub fn main() void { pub fn main() void {
var a: u32 = addFive(44) catch 0; const a: u32 = addFive(44) catch 0;
var b: u32 = addFive(14) catch 0; const b: u32 = addFive(14) catch 0;
var c: u32 = addFive(4) catch 0; const c: u32 = addFive(4) catch 0;
std.debug.print("a={}, b={}, c={}\n", .{ a, b, c }); 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 // What's really nice is that you can use a switch statement as an
// expression to return a value. // expression to return a value.
// //
// var a = switch (x) { // const a = switch (x) {
// 1 => 9, // 1 => 9,
// 2 => 16, // 2 => 16,
// 3 => 7, // 3 => 7,

View file

@ -9,7 +9,7 @@
// @enumToInt(). We'll learn about builtins properly in a later // @enumToInt(). We'll learn about builtins properly in a later
// exercise. // exercise.
// //
// var my_stuff: u8 = @enumToInt(Stuff.foo); // const my_stuff: u8 = @enumToInt(Stuff.foo);
// //
// Note how that built-in function starts with "@" just like the // Note how that built-in function starts with "@" just like the
// @import() function we've been using. // @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 // Please threaten the result so that answer is either the
// integer value from deepThought() OR the number 42: // integer value from deepThought() OR the number 42:
var answer: u8 = result; const answer: u8 = result;
std.debug.print("The Ultimate Answer: {}.\n", .{answer}); std.debug.print("The Ultimate Answer: {}.\n", .{answer});
} }

View file

@ -78,7 +78,7 @@ pub fn main() void {
}; };
var aliens_alive = aliens.len; 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. // We'll keep checking to see if we've killed all the aliens yet.
while (aliens_alive > 0) { while (aliens_alive > 0) {

View file

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

View file

@ -43,7 +43,7 @@ pub fn main() void {
// //
// We'll convert this weight from tons to kilograms at a // We'll convert this weight from tons to kilograms at a
// conversion of 907.18kg to the ton. // 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 // By default, float values are formatted in scientific
// notation. Try experimenting with '{d}' and '{d:.3}' to see // notation. Try experimenting with '{d}' and '{d:.3}' to see

View file

@ -1,4 +1,4 @@
13c13 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 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 22c22
< fn addTwenty(n: u32) ??? { < fn addTwenty(n: u32) ??? {
--- ---

View file

@ -1,4 +1,4 @@
32c32 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 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;