Skip to main content
Rust Programming
CHAPTER 03 Beginner

Rust Syntax and First Program

Updated: May 18, 2026
5 min read

# CHAPTER 3

Rust Syntax and First Program

1. Chapter Introduction

Every programming journey begins with a "Hello, World!" application. In this chapter, we will write our first Rust program and dissect it line by line. We will learn how Rust uses functions, understand what a "Macro" is, explore how to format output, and learn the rules of Rust's syntax, such as semicolons and code blocks.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Write and understand a basic Rust program.
  • Understand the role of the main function.
  • Differentiate between a regular function and a Macro (like println!).
  • Write single-line and multi-line comments.
  • Understand Rust's block scoping with curly braces {}.

3. The "Hello, World!" Program

Let's look at the default code generated by cargo new. Open the src/main.rs file.
rust
123
fn main() {
    println!("Hello, world!");
}

Let's break this down step-by-step:

#### A. The fn Keyword In Rust, fn stands for "function". It is used to declare a new function.

#### B. The main Function main is special. It is the entry point of every executable Rust program. When you run your program, the operating system looks for the main function and executes the code inside it first.

#### C. Curly Braces {} The curly braces {} define a "block" of code. Everything between the opening { and the closing } belongs to the main function.

#### D. The println! Macro println! prints text to the screen and adds a new line at the end. Notice the exclamation mark (!)? That means println! is a Macro, not a regular function. Macros in Rust are essentially code that writes more code for you during compilation. We will explore macros deeply later, but for now, remember: if you see a !, it's a macro!

#### E. The Semicolon ; Rust is an expression-based language, but it uses semicolons ; to end statements. Forgetting a semicolon is a common beginner mistake and will cause a compiler error.

4. Running the Program

As we learned in Chapter 2, you can run this by opening your terminal in the project folder and typing:
bash
1
cargo run

*Output:*

1
Hello, world!

5. Comments in Rust

Comments are notes for yourself or other developers. The compiler completely ignores them.

Single-line comments start with //:

rust
1234
// This is a comment. It explains the code below.
fn main() {
    println!("Hello"); // This is an inline comment
}

Multi-line comments (block comments) use /* and */:

rust
12345678
/* 
   This is a multi-line comment.
   It can span across several lines.
   Useful for temporarily disabling large chunks of code.
*/
fn main() {
    println!("Hello");
}

6. Formatting Strings

println! is incredibly powerful. You can use curly braces {} inside the string as placeholders to print data.
rust
12345678910111213
fn main() {
    // Basic placeholder
    println!("My favorite number is {}", 42);
    
    // Multiple placeholders
    println!("My name is {} and I am {} years old", "Alice", 28);
    
    // Positional arguments (0-indexed)
    println!("{0} is a language, {1} is a language, but {0} is safer.", "Rust", "C++");
    
    // Named arguments
    println!("{subject} is {adjective}", subject="Rust", adjective="fast");
}

7. Mini Project: Simple Output App

Let's combine what we know into a small profile printer.
rust
12345678910111213141516
fn main() {
    // Print a header
    println!("======================");
    println!("   DEVELOPER PROFILE  ");
    println!("======================");
    
    // Print formatted data
    println!("Name: {}", "John Doe");
    println!("Role: {}", "Backend Engineer");
    println!("Language: {}", "Rust");
    
    /* 
       End of program.
       The OS will close the app now.
    */
}

8. Common Mistakes

  • Forgetting the ! on println: If you write println("Hello");, the compiler will complain that the function println does not exist. It must be println!("Hello");.
  • Forgetting the semicolon ;: If you leave off the semicolon, the compiler might think the statement hasn't finished, leading to confusing errors on the next line.
  • Using single quotes for strings: In Rust, "Hello" (double quotes) is a String. 'A' (single quotes) is a single Character. You cannot use single quotes for multiple letters!

9. Best Practices

  • Indentation: Standard Rust style uses 4 spaces for indentation. cargo fmt will automatically format this for you.
  • Snake Case: Rust strongly prefers snakecase (lowercase with underscores) for function and variable names, not camelCase. (e.g., fn calculatetax() instead of fn calculateTax()).

10. Exercises

  1. 1. Write a program that uses a multi-line comment at the top to describe the file.
  1. 2. Use println! to output your favorite movie, book, and food using {} placeholders.
  1. 3. Try removing a semicolon and running cargo run to see what a Rust compiler error looks like.

11. MCQs with Answers

Question 1

What is the entry point of a Rust executable program?

Question 2

What keyword is used to declare a function in Rust?

Question 3

How do you know if you are calling a Macro instead of a regular function?

# Answer: b) It ends with an exclamation mark !.
Question 4

What is the correct syntax to print "Hello"?

Q5. How do you write a single-line comment in Rust? a) # Comment b) <!-- Comment --> c) // Comment d) /* Comment Answer: c) // Comment.
Question 6

What does println!("I have {} apples", 5); output?

Question 7

What character is used to end a statement in Rust?

Question 8

What is the standard naming convention for functions in Rust?

Question 9

Which brackets define a block of code (like a function body)?

Q10. Can you use single quotes ' ' to define a string of words in Rust? a) Yes b) No, single quotes are strictly for single Characters Answer: b) No, single quotes are strictly for single Characters.

12. Interview Questions

  • Q: What is the difference between a function and a macro in Rust?
  • Q: Why does Rust use println! as a macro instead of a standard function? (Hint: standard functions in Rust cannot take a variable number of arguments, but macros can generate code to handle it).

13. FAQs

  • Why does the first compile take longer? The first time you run cargo run, Rust compiles the standard library and your code. Subsequent runs only recompile the parts you changed, which is much faster.

14. Summary

Rust's basic syntax is clean and predictable, sharing similarities with C and JavaScript. The main function serves as the application's starting point, and macros like println! provide powerful string formatting. Semicolons and curly braces organize the structure, while comments help document the code.

15. Next Chapter Recommendation

Printing text is great, but programming is about managing data. In Chapter 4: Variables, Constants, and Data Types, we will learn how to store data in memory, explore Rust's strict Type System, and discover the concept of "Immutability by default".

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·