Rust Syntax and First Program
# 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
mainfunction.
-
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 bycargo new. Open the src/main.rs file.
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:*Output:*
5. Comments in Rust
Comments are notes for yourself or other developers. The compiler completely ignores them.Single-line comments start with //:
Multi-line comments (block comments) use /* and */:
6. Formatting Strings
println! is incredibly powerful. You can use curly braces {} inside the string as placeholders to print data.
7. Mini Project: Simple Output App
Let's combine what we know into a small profile printer.8. Common Mistakes
-
Forgetting the
!onprintln: If you writeprintln("Hello");, the compiler will complain that the functionprintlndoes not exist. It must beprintln!("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 fmtwill automatically format this for you.
-
Snake Case: Rust strongly prefers
snakecase(lowercase with underscores) for function and variable names, notcamelCase. (e.g.,fn calculatetax()instead offn calculateTax()).
10. Exercises
- 1. Write a program that uses a multi-line comment at the top to describe the file.
-
2.
Use
println!to output your favorite movie, book, and food using{}placeholders.
-
3.
Try removing a semicolon and running
cargo runto see what a Rust compiler error looks like.
11. MCQs with Answers
What is the entry point of a Rust executable program?
What keyword is used to declare a function in Rust?
How do you know if you are calling a Macro instead of a regular function?
!.
What is the correct syntax to print "Hello"?
What does println!("I have {} apples", 5); output?
What character is used to end a statement in Rust?
What is the standard naming convention for functions in Rust?
Which brackets define a block of code (like a function body)?
' ' 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. Themain 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.