rust : add variables info.
This commit is contained in:
parent
832cf46d10
commit
295198f75d
1
dev/lang/lua/TODO.md
Normal file
1
dev/lang/lua/TODO.md
Normal file
|
@ -0,0 +1 @@
|
|||
# Write about Lua.
|
11
dev/lang/rust/function.md
Normal file
11
dev/lang/rust/function.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
# Functions.
|
||||
|
||||
# Declare.
|
||||
|
||||
Declare functions using `fn` keyword.
|
||||
|
||||
```rust
|
||||
fn main() {}
|
||||
|
||||
fn main(x: i32) {}
|
||||
```
|
|
@ -1 +0,0 @@
|
|||
TODO: add rust wiki.
|
43
dev/lang/rust/variable.md
Normal file
43
dev/lang/rust/variable.md
Normal file
|
@ -0,0 +1,43 @@
|
|||
# Variables.
|
||||
|
||||
# Definition.
|
||||
|
||||
## Immutable.
|
||||
|
||||
Use the `let` keyword to start declaration. Types are specified after the variable name.
|
||||
|
||||
```rust
|
||||
let x: i32 = 10;
|
||||
```
|
||||
|
||||
Type definition is optional, so you can drop it. I suggest specifying the type for global/class/etc variables, but dropping it in local scope like functions.
|
||||
|
||||
```rust
|
||||
let x = "This is &str"; // Type is detected automatically: &str.
|
||||
```
|
||||
|
||||
## Mutable.
|
||||
|
||||
Add `mut` keyword after `let`.
|
||||
|
||||
```rust
|
||||
let mut x: i32 = 10;
|
||||
x = 12;
|
||||
```
|
||||
|
||||
## Constants.
|
||||
|
||||
To declare constant, use the `const` keyword instead of `let`. Constant values always require type definition.
|
||||
|
||||
```rust
|
||||
const NUMBER: i32 = 10;
|
||||
```
|
||||
|
||||
# Shadowing.
|
||||
|
||||
You can redefine a variable with the same name but with a different type. This is useful if you cast something, but want to keep the same variable name. To do this, just declare the variable again.
|
||||
|
||||
```rust
|
||||
let x: &str = "&str"; // &str.
|
||||
let x: i32 = 10; // i32.
|
||||
```
|
Loading…
Reference in a new issue