/* program entry point */
fn main() {
println!("Hello, world!"); //macro
}
There is no better time than now to introduce you the basic concepts and notations of Rust syntax. We will show you on example then we make notes to explain the concept. We explain them as simple as possible so you can learn Rust as your first programming language.
In Rust there is a concept of code block. It is a compact region of code separated by squiggly brackets: {....}. In other languages we start a block using "begin" and terminate with "end" keywords. Having brackets used as block of code makes Rust a little bit harder to understand, but we know how to use spaces and new lines to organize our code.
In Rust the indentation is not relevant but recommended. What is relevant is that we can have nested blocks of code. The outer block can contain one or more inner blocks. Each block of code define a local scope.
The variable declaration in Rust is done using keyword "let" and symbol "=". Once a variable is initialized we call this "binding". So a variable is bound to it's value. A variable that is not bound can't be used. To declare a type for a variable we use symbol ":" after the variable name.
A variable is an identifier or a name that can start with "_" or any alphabetic character but not a special character or a number. So this is almost like any other language.To make a variable mutable we declare it using additional keyword "mut". If you forget this, your variable is actually constant.
/* define variables */
let x: i32 = 5;
let mut y: i32 = 10;
y = 15; //now we can use assignment
x = 15; //this will not work since x is immutable
These are ASCII characters or group of two characters used to separate identifiers or literals. Parenthesis can group elements together in collections or complex expressions.
Symbol | Description |
---|---|
: | define type |
; | statement separator, define array element type |
, | enumeration |
. | dot operator or decimal separator |
\ | string continuation or escape code |
-> | define result type |
> | relation operator: greater then |
< | relation operator: less then |
// | single line comment (can be used at start of line or end of line after ";" |
/*...*/ | block comment. Other variations: /** .... */ or /*! .... */ each with slightly different purpose. |
& | borrow |
* | raw pointer |
.. | range of numbers or update operator for struct literals |
-> | define result type of a function |
@ | pattern bindings |
_ | ignored |
? | error propagation symbol |
=> | used in match patterns |
... | inclusive range pattern |
[...] | array literal, vector literal |
{...} | block of code, struct literal, interpolation placeholder |
(...) | tuple, expression, parameters |
<...> | declare generic data types |
# | meta (compiler directive) |
$ | macro substitution |
:: | file based scoping operator similar to "." |
m! | macro notation for example: print!(). It is also used as logical operator. Do not confuse the two. |
'...' | character literal |
"..." | delimiters for string literal |
You should avoid using next keywords as identifiers. Reserved keywords are used in language statements. You do not have to memorize all of them now. Just read them once to get a felling for what you will learn in the future from our tutorial. I have counted 35 keywords. This makes Rust a lighter language, compared with 50 keywords for Java.
as | box | break |
const | continue | crate |
else | enum | extern |
false | fn | for |
if | impl | in |
let | loop | match |
mod | move | mut |
pub | ref | return |
self | Self | static |
struct | super | trait |
true | type | unsafe |
use | where | while |
Read next: Data Types