First look at this simple program
1. Strange keywords
fn = function function
mut = Variability mutability, the opposite of immutability invariance.
2. Variable Definition
This and other languages are not the same, ordinary language variables default variable, if they have not become labeled as readonly
Some languages are they not immutable this design, all variables can be changed.
the rust, the let Immutability defined variables, immutable, similar to other languages readonly
Therefore, x in the figure read-only variables, immutable
Y may be assigned in FIG.
Rust and allows the definition of cover, FIG x defined twice, the previous definition will be overwritten.
rust called attention to the clarity of the language, but do not allow the definition to cover increased uncertainty sex? I just started learning, understanding is not deep, the problem I first stood, perhaps later have the answer.
3. string formatted output
{} Format output by the x, y
String design rust is a little awkward, access to information display
There rust char, and char is four bytes, i.e. utf32
But String is not vec <char> but vec <u8> is the string is utf8
So string can not be accessed as char list, but by "नमस्ते". Chars () function, into a list of char
Here are key records about rust char is utf32, string is utf8
Because there can not be a character char utf32 represented, it still can not be considered a char is a character. But it has covered 99.99%, most of our Chinese text a word utf16 enough expressed, beyond those words difficult to meet.
So that we can write a more complicated procedure
3.for cycle
foreach loop only for the rust one form, there is no kind of language c for (var i = 0; i <10; i ++) in the form of
1..10 iterator is from 1 to 10, excluding 10
His statement is not for small parentheses
continue and break out of the loop can be used, and other similar languages
4.if expression
if expression is similar to other languages, and also, if different portions of the conditional expression without parentheses may be, you may have.
However, braces can not be omitted
Allowed if (y == 3) continue; this wording
The loop tag
rust provides recycling label concept
Cycle can take a tag, break and continue to do so, you can directly specify which one to get out of the cycle, in the case of nested loops so that the logic can be simplified, but also the behavior closer to machine language.
Combined wasm of loops designed to look, ah, you get rust and wasm certainly a lot of blending.
Again a little complicated procedures, it outputs a multiplication tables
6.USE statement
Began to write complex programs will have to rely on the library, where we used the stdout rust standard library
:: io :: * can simply solve the problem through use std
7. string related
Here is connected via a macro format string, then as_bytes () he becomes a
& [U8] type, should be stdout () write ();. Requirement of this type
8.expect
expect ( "") can not write this part, this is a package of common rust, return result, this function may be a case of failure
Wherein when the output string .expect ( "") denotes failure
Rust are many ways to return result, may fail. Of course, also be used if the judge result
.expect ( "") is a fast exception handling rust offered.
Also very good
The first rust program, complete