Comments

There are two kinds of comments in Sway.

Regular Comments

Regular comments are broken down into two forms of syntax:

  • // comment
  • /* comment */

The first form starts after the two forward slashes and continues to the end of the line.

Comments can be placed on multiple lines by starting each line with // and they can be placed at the end of some code.

    // imagine that this line is twice as long
    // and it needed to be split onto multiple lines
    let baz = 8; // Eight is a good number

Similarly, the second form continues to the end of the line and it can also be placed at the end of some code.

    /*
        imagine that this line is twice as long
        and it needed to be split onto multiple lines
    */

Documentation Comments

Documentation comments start with three forward slashes /// and are placed on top of functions or above fields e.g. in a struct.

Documentation comments are typically used by tools for automatic documentation generation.

/// Data structure containing metadata about product XYZ
struct Product {
    /// Some information about field 1
    field1: u64,
    /// Some information about field 2
    field2: bool,
}

/// Creates a new instance of a Product
///
/// # Arguments
///
/// - `field1`: description of field1
/// - `field2`: description of field2
///
/// # Returns
///
/// A struct containing metadata about a Product
fn create_product(field1: u64, field2: bool) -> Product {
    Product { field1, field2 }
}