Rust for the Novice Programmer/Comments

Comments

edit

We're beginning to have a few lines in our code, and sometimes it can awkward to organise properly. One useful tip is that you can put notes or annotations to your code that explains what it does. This is done with comments, with which there are two ways in Rust:

  1. using '//' before a line means that the rest of the line is ignored by the compiler
  2. using '/*' means that all the rest of the code will be ignored by the compiler until it finds '*/'

For example:

 fn main() {
     //the next section calculates x^2 + 5 and prints it out
     let mut x = 10;
     x *= x;
     x += 5;
     println!("{}", x);
 }

Here the comment is used to annotate what the next section does. We can also use these comments to stop code from being run temporarily without removing it entirely. For example:

 fn main() {
     let mut x = 10;
     /*x *= x;
     x += 5;*/
     println!("{}", x);
 }

Here the value 10 will be printed since the two lines have the /* */ comments around them so they are not run.

Comments are just generally useful and will be used in later sections to annotate code.

Next: More numbers and data types