Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Zig Comments

Zig Comments

Comments are not processed by the compiler; they are only used to add explanations and descriptions in the code, helping developers understand the code logic.

In Zig, there are two forms of comments: single-line comments and multi-line comments.

1. Single-line Comments

Single-line comments start with //, and the comment content runs from // to the end of the line.

Example

const

std

=

@import

(

"std"

)

;

// This is a single-line comment

pub fn main

(

)

void

{

    std.

debug

.

print

(

"Hello, World!\n"

, .

{

}

)

;

}

2. Multi-line Comments

Multi-line comments start with /* and end with */, and the comment content can span multiple lines.

Example

const

std

=

@import

(

"std"

)

;

/*
  This is a multi-line comment
  can span multiple lines
*/

pub fn main

(

)

void

{

    std.

debug

.

print

(

"Hello, World!\n"

, .

{

}

)

;

}

Comment usage

The following is a complete example containing single-line and multi-line comments, demonstrating how to add comments in code.

Example

const

std

=

@import

(

"std"

)

;

// Main function

pub fn main

(

)

void

{

   

// Call the standard library's debug.print function to print "Hello, World!"

    std.

debug

.

print

(

"Hello, World!\n"

, .

{

}

)

;

   

   

/*
      This code is used to demonstrate the basic syntax of Zig
      including function definition, standard library usage, and comments
    */

   

   

const

a

:

i32

=

10

;

// Define an integer constant a with value 10

   

const

b

:

i32

=

20

;

// Define another integer constant b with value 20

   

// Call the add function and print the result

   

const

result

=

add

(

a, b

)

;

    std.

debug

.

print

(

"Result: {}\n"

, .

{

result

}

)

;

}

// A simple addition function

fn add

(

a

:

i32, b

:

i32

)

i32

{

   

return

a

b

;

}