1. Hello, world!
First of all, printing hello world! in rust is as simple as in python:
fn main(){
println!("Hello, world!");
}
But there are important things even in this simple code:
- RUST always needs main function named main().
- We can define function with
fn println!is not a function, is a macro: macro is a piece of code that generates another piece of code.
Note : Macro
Macros are expanded at compile time, so that the actual generated codes will replace the macro before the program is executed. When you want to use macro to generate code in compile level, you need to put!right after the name of macro. You can also make your codes be a macro. However, there is no free lunch. Macro makes you comfortable in writing a program or save execution time (by avoiding function call) but your compile time might also increase as much you use macro.
And before talking about data structures and syntaxes of Rust, I will put the general naming rules.
| Item | Convention |
|---|---|
| Modules | snake_case |
| Types / Traits / Enum / Struct | PascalCase |
| Macros / Functions / Methods | snake_case |
| Local variables | snake_case |
| Static variables / Constants | SCREAMING_SNAKE_CASE |
| Type parameters | usually single uppercase letter: T or consice PascalCase |
| Lifetimes | usually a single letter: ‘a or short lowercase : ‘de, ‘src |