Comments
Comments help document your code and explain complex logic.
Single-Line Comments
Hash Comments
# This is a comment
let x = 10 # Inline commentDouble Slash Comments
// This is also a comment
let y = 20 // Inline commentMulti-Line Comments
/*
This is a multi-line comment.
It can span multiple lines.
Useful for longer explanations.
*/
let z = 30Documentation Comments
Function Documentation
# Calculates the sum of two numbers
#
# Args:
# a: First number
# b: Second number
#
# Returns:
# Sum of a and b
fn add(a: i32, b: i32) -> i32 {
return a + b
}Module Documentation
# Math Module
#
# Provides basic arithmetic operations:
# - Addition
# - Subtraction
# - Multiplication
# - Division
module mathComment Best Practices
Good Comments
# Calculate factorial using recursion
# Base case: n <= 1 returns 1
fn factorial(n: i32) -> i32 {
if n <= 1 {
return 1 # Base case
}
return n * factorial(n - 1) # Recursive case
}Avoid Obvious Comments
# Bad: Obvious comment
let x = 10 # Set x to 10
# Good: Explains why
let timeout = 30 # 30 seconds - enough for slow networksCommented Code
# Temporarily disabled for debugging
# let result = expensive_calculation()
# Use this instead
let result = quick_calculation()TODO Comments
# TODO: Add error handling
# TODO: Optimize this function
# FIXME: Handle edge case when x is negative
# NOTE: This works but could be improvedExamples
Well-Documented Function
# Validates user input and returns sanitized value
#
# This function:
# - Trims whitespace
# - Removes special characters
# - Converts to lowercase
#
# Args:
# input: Raw user input string
#
# Returns:
# Sanitized string ready for processing
fn sanitize(input: str) -> str {
# Implementation coming soon
return input
}