-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move variables / const examples to separate .rs files
- Loading branch information
Showing
4 changed files
with
36 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
fn main() { | ||
// destructuring tuples | ||
let (_x, _y, _z) = (1, 2, 3); | ||
|
||
// destructuring structs | ||
struct Point { | ||
x: i32, | ||
y: i32, | ||
} | ||
|
||
let p = Point { x: 0, y: 7 }; | ||
let Point { x: _a, y: _b } = p; // a = 0, b = 7 | ||
let Point { x, y } = p; // simpler | ||
let _ = (x, y); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
fn main() { | ||
let x = 5; // x is immutable | ||
|
||
let x = x + 1; // redefines x | ||
println!("{x}"); | ||
|
||
let x = "example"; // the type can change | ||
println!("{x}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#![allow(unused)] | ||
fn main() { | ||
// `const` is set to a constant expression; the type must be annotated | ||
const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3; | ||
|
||
let apples = 5; // immutable variable | ||
|
||
let mut guess = String::new(); // mutable variable | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,19 @@ | ||
# Variables and Constants | ||
|
||
```rust,editable | ||
fn main() { | ||
const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3; // set only to a constant expression; type must be annotated | ||
let apples = 5; // immutable | ||
let mut guess = String::new(); // mutable | ||
} | ||
{{#include ../../deps/examples/vars_and_consts.rs}} | ||
``` | ||
|
||
## Shadowing | ||
|
||
```rust,editable | ||
fn main() { | ||
let x = 5; | ||
let x = x + 1; // redefines x; type can change | ||
} | ||
{{#include ../../deps/examples/shadowing.rs}} | ||
``` | ||
|
||
## Destructuring | ||
|
||
```rust,editable | ||
fn main() { | ||
// destructuring tuples | ||
let (x, y, z) = (1, 2, 3); | ||
// destructuring structs | ||
struct Point{ x: i32, y: i32, } | ||
let p = Point { x: 0, y: 7 }; | ||
let Point { x: a, y: b } = p; // a = 0, b = 7 | ||
let Point { x, y } = p; // simpler | ||
} | ||
{{#include ../../deps/examples/destructuring.rs}} | ||
``` | ||
|
||
Starting the name of a variable with an underscore silences unused variable warnings. |