Beginner's Guide to Zig Part 2 - Slices, Printing, If statements and loops

Поділитися
Вставка
  • Опубліковано 21 лис 2024

КОМЕНТАРІ • 11

  • @pietraderdetective8953
    @pietraderdetective8953 4 місяці тому +1

    Love the Zig content! I particularly love how C and Zig are 1:1.

  • @ericmisha
    @ericmisha 21 день тому

    (assuming a,b,c,d are booleans for the sake of simplicity)
    Instead of:
    if (a || b)
    use:
    if (a or b)
    Instead of:
    if (a && b)
    use:
    if (a and b)
    You can also combined them as you'd expect, for example:
    if ((a and b) or (c and d))

  • @zigzig-b9p
    @zigzig-b9p 3 місяці тому +1

    Hi
    Thanks for this video. I have a few comments. If I have got the wrong end of the stick please advise.
    zig does not infer types like basic or python.
    const my_const=10; is syntax sugar for const my_const:comptime_int=10;
    You see a const is a comptime variable. The reason it does not do that for a var is because a var can exist in runtime and therefore needs a type.
    If however you tell the compiler that var is comptime it works fine. See below
    const std = @import("std"); // this loads the standard library for zig
    pub fn main() !void {
    //const my_const:comptime_int=10; // syntax sugar below
    const my_const=10; // this is a comptime variable; Therefore it becomes a comptime_int
    //
    // var my_var=10; // Error: variable of type 'comptime_int' must be const or comptime
    comptime var my_var=10; // because we have told the compiler that var is comptime it becomes a comptime_int
    // show me the type
    std.debug.print("my_const type = {any}
    ", .{@TypeOf(my_const) } );
    std.debug.print("my_var type = {any}
    ", .{@TypeOf(my_var) } );
    // tidy up so that the code compiles
    //_=my_const; // need to give const home
    my_var+=1; // need to mutate var
    } // end of main
    You mentioned slices.
    const std = @import("std"); // this loads the standard library for zig
    pub fn main() !void {
    // the const not_slice is a single pointer, to a read only part of ram ie *const [11:0]u8
    // The read only ram is an array [0]=H, [1]=e, [2]=l etc
    // It's null terminated ie :0. It has a length of 11
    const not_slice="Hello world";
    std.debug.print("Not_slice type = {any}, it_contains {s}
    ", .{@TypeOf(not_slice), not_slice} ); //
    // slice is defined as a fat pointer ie []const u8. The const here means read only ram
    //const slice:[] const u8 = not_slice[0..4];
    const slice:[] const u8 = not_slice; // this would be the same as [0..]
    std.debug.print("Slice type = {any}, it_contains {s}
    ", .{@TypeOf(slice), slice} ); //
    // Lets print a chracter ie {c}
    std.debug.print( "{c}
    ", .{not_slice[0]} ); //
    std.debug.print( "{c}
    ", .{not_slice[1]} ); //
    // lets print a string, which is not a slice
    std.debug.print( "{s}
    ", .{not_slice} ); //
    }
    Hopefully this is all correct.

    • @CodingWithTom-tn7nl
      @CodingWithTom-tn7nl  3 місяці тому

      You're correct about constant variables and inferring.
      On slices, maybe there is a semantic difference but I believe they are basically the same:
      const string1:*const [11:0]u8 = "Hello world";
      const string2:*const [11]u8 = "Hello world";
      const string3:[]const u8 = "Hello world";
      std.debug.print("{} {} {}
      ", .{@TypeOf(string1), @TypeOf(string2), @TypeOf(string3)});
      std.debug.print("{s} {s} {s}
      ",.{string1, string2, string3});
      std.debug.print("{c} {c} {c}
      ",.{string1[0], string2[0], string3[0]});
      const length1 = string1.len;
      const length2 = string2.len;
      const length3 = string2.len;
      std.debug.print("{} {} {}
      ",.{length1, length2, length3});
      You print these both with {s} which shows they are both slices. In string1 and string2 you simply specify the length.
      You can even write it without the ":0" and it still does the same thing.

  • @TheFreshMakerHD
    @TheFreshMakerHD 4 місяці тому +2

    7:00 to print a slice of numbers (decimal, int, etc) is to use {d} in the formatter ex: std.debug.print("{d}", .{number_slice});

  • @nikolazivkovic9541
    @nikolazivkovic9541 4 місяці тому +1

    13:10 you could have used keyword 'and' instead of &&

    • @CodingWithTom-tn7nl
      @CodingWithTom-tn7nl  4 місяці тому +1

      I wasn't aware of that keyword when I made the video, I didn't find it mentioned anywhere in any guides. I only recently found 'and' and 'or' randomly in a function in the standard library. I'll probably make a future video including those.

  • @dabunnisher29
    @dabunnisher29 4 місяці тому

    Sorry to say this but the speed that you swing up and down makes it hard for viewers like me see what you are talking about.

    • @CodingWithTom-tn7nl
      @CodingWithTom-tn7nl  4 місяці тому

      Sorry about that, soon I'll make all the code available in a github page so people can look themselves if they like