Comments in Rust Programming


Rust Programming


Rust Programming



Comments are a way to improve the readability of a program. Comments can be used to include additional information about a program like author of the code, hints about a function/ construct, etc. The compiler ignores comments.

Rust supports the following types of comments: 

  • Single-line comments ( // ) : Any text between a // and the end of a line is treated as a comment
  • Multi-line (Block) comments (/* */) : These comments may span multiple lines.

Single Line Comments


In Rust, we use two forward slashes // to create a line comment. For example,

Input Example 1

fn main () {
    // declare a variable
    let x = 1;
    println!("x = {}", x);
}{codeBox}

Input Example 2

fn main() {
    let x = 1; // declare a variable
    println!("x = {}", x);
}{codeBox}

Output

x = 1{codeBox}

The comment extends up to the end of the line and is also known as single-line comments.


Multi line (Block) Comments


The symbol /*...*/ to denote the block comment. It starts with /* and ends with */. For example

Input

fn main() {
    /*
    declare a variable
    and assign value to it
    */
    let x = 1;
    println!("x = {}", x);
}{codeBox}

Output

x = 1{codeBox}

You can see the block comment extends for multiple lines.


Credits & Copyrights©: Javatpoints & programiz

Post a Comment