This repository has been archived by the owner on Mar 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrew Hobden
committed
May 11, 2017
1 parent
87756a3
commit 81a888a
Showing
15 changed files
with
298 additions
and
0 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,100 @@ | ||
# Syntax | ||
[Table of Contents](toc/english.html) | ||
|
||
--- | ||
|
||
Rust has a C-style syntax with influences from functional languages. | ||
|
||
Specific functionality will be covered later. | ||
|
||
--- | ||
|
||
## Basics | ||
|
||
<pre><code data-source="chapters/shared/code/syntax/1.rs" data-trim="hljs rust" class="lang-rust"></code></pre> | ||
|
||
--- | ||
|
||
## `if` and `if let` | ||
|
||
<pre><code data-source="chapters/shared/code/syntax/2.rs" data-trim="hljs rust" class="lang-rust"></code></pre> | ||
|
||
--- | ||
|
||
## `match` | ||
|
||
<pre><code data-source="chapters/shared/code/syntax/3.rs" data-trim="hljs rust" class="lang-rust"></code></pre> | ||
|
||
--- | ||
|
||
## `loop` and `while` | ||
|
||
<pre><code data-source="chapters/shared/code/syntax/4.rs" data-trim="hljs rust" class="lang-rust"></code></pre> | ||
|
||
--- | ||
|
||
## `for` and `while let` | ||
|
||
<pre><code data-source="chapters/shared/code/syntax/5.rs" data-trim="hljs rust" class="lang-rust"></code></pre> | ||
|
||
--- | ||
|
||
## `struct`, `type`, and `enum` | ||
|
||
<pre><code data-source="chapters/shared/code/syntax/6.rs" data-trim="hljs rust" class="lang-rust"></code></pre> | ||
|
||
--- | ||
|
||
## `impl` and `trait` | ||
|
||
<pre><code data-source="chapters/shared/code/syntax/7.rs" data-trim="hljs rust" class="lang-rust"></code></pre> | ||
|
||
--- | ||
|
||
## Borrowing | ||
|
||
<pre><code data-source="chapters/shared/code/syntax/8.rs" data-trim="hljs rust" class="lang-rust"></code></pre> | ||
|
||
--- | ||
|
||
## Lifetimes | ||
|
||
`'foo` can be used to describe the lifetime of borrows. | ||
|
||
<pre><code data-source="chapters/shared/code/syntax/9.rs" data-trim="hljs rust" class="lang-rust"></code></pre> | ||
|
||
--- | ||
|
||
## Scopes | ||
|
||
Rust is block scoped. Scopes can return values. | ||
|
||
<pre><code data-source="chapters/shared/code/syntax/10.rs" data-trim="hljs rust" class="lang-rust"></code></pre> | ||
|
||
--- | ||
|
||
## Closures | ||
|
||
<pre><code data-source="chapters/shared/code/syntax/11.rs" data-trim="hljs rust" class="lang-rust"></code></pre> | ||
|
||
--- | ||
|
||
## Generics | ||
|
||
<pre><code data-source="chapters/shared/code/syntax/12.rs" data-trim="hljs rust" class="lang-rust"></code></pre> | ||
|
||
Generally the `where` syntax is preferred. | ||
|
||
--- | ||
|
||
## `use` and `mod` | ||
|
||
<pre><code data-source="chapters/shared/code/syntax/13.rs" data-trim="hljs rust" class="lang-rust"></code></pre> | ||
|
||
--- | ||
|
||
## On Semicolons | ||
|
||
A line without a semi-colon implictly returns. Don't worry, the compiler will tell you if you forget it. | ||
|
||
<pre><code data-source="chapters/shared/code/syntax/14.rs" data-trim="hljs rust" class="lang-rust"></code></pre> |
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,17 @@ | ||
// Function declaration | ||
fn add_them(first: i32, second: i32) -> i32 { | ||
first + second | ||
} | ||
|
||
fn main() { | ||
// Mutable variable | ||
let mut some_value = 1; | ||
// Immutable, explict type | ||
let explicitly_typed: i32 = 1; | ||
|
||
// Function call | ||
some_value = add_them(some_value, explicitly_typed); | ||
|
||
// Macro, note the ! | ||
println!("{}", some_value) | ||
} |
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,10 @@ | ||
fn main() { | ||
let foo = 1; | ||
let bar = { | ||
// Shadows earlier declaration. | ||
let foo = 2; | ||
foo | ||
}; | ||
println!("{}", foo); | ||
println!("{}", bar); | ||
} |
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() { | ||
// Shorthand | ||
let value = Some(1).map(|v| v + 1); | ||
// With a block | ||
let value = Some(1).map(|v| { | ||
v + 1 | ||
}); | ||
// Explict return type | ||
let value = Some(1).map(|v| -> i32 { | ||
v + 1 | ||
}); | ||
// Declared | ||
let closure = |v| v + 1; | ||
let value = Some(1).map(closure); | ||
} |
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,21 @@ | ||
// Inline syntax | ||
fn generic_inline<S: AsRef<str>>(thing: S) -> S { | ||
thing | ||
} | ||
|
||
// Where syntax | ||
fn generic_where<Stringish>(thing: Stringish) -> Stringish | ||
where Stringish: AsRef<str> { | ||
thing | ||
} | ||
|
||
// Enums too! | ||
struct GenericStruct<A> { | ||
value: A, | ||
} | ||
|
||
fn main() { | ||
let foo = "foo"; | ||
generic_inline(foo); | ||
generic_where(foo); | ||
} |
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,14 @@ | ||
use foo::foo; | ||
|
||
mod foo { | ||
pub fn foo() { | ||
// ... | ||
} | ||
} | ||
|
||
// Will try to open `./bar.rs` relative to this file. | ||
pub mod bar; | ||
|
||
fn main() { | ||
foo() | ||
} |
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,8 @@ | ||
fn do_thing() -> bool { | ||
"Forgotten semicolon" | ||
true | ||
} | ||
|
||
fn main() { | ||
do_thing() | ||
} |
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,13 @@ | ||
fn main() { | ||
let value = 2; | ||
if value % 2 == 0 { | ||
// ... | ||
} else if value == 5 { | ||
// ... | ||
} else { /* ... */ } | ||
|
||
let maybe_value = Some(2); | ||
if let Some(value) = maybe_value { | ||
// ... | ||
} else { /* ... */ } | ||
} |
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,14 @@ | ||
fn main() { | ||
let maybe_value = Some(2); | ||
match maybe_value { | ||
Some(value) if value == 2 => { | ||
// ... | ||
} | ||
Some(value) => { | ||
// ... | ||
}, | ||
None => { | ||
// ... | ||
}, | ||
} | ||
} |
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() { | ||
let mut value = 0; | ||
// Loop with break | ||
loop { | ||
if value >= 10 { | ||
break; | ||
} | ||
value += 1; | ||
} | ||
// Break on conditional | ||
while value <= 10 { | ||
value += 1; | ||
// ... | ||
} | ||
} |
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,12 @@ | ||
fn main() { | ||
// Loop over iterator | ||
let range = 0..10; | ||
for i in range { | ||
// ... | ||
} | ||
// while let | ||
let mut range = 0..10; | ||
while let Some(v) = range.next() { | ||
// ... | ||
} | ||
} |
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,16 @@ | ||
struct Empty; | ||
|
||
struct WithFields { | ||
foo: i32, | ||
bar: Choice, | ||
} | ||
|
||
type Explanation = String; | ||
|
||
enum Choice { | ||
Yes, | ||
No, | ||
Maybe(Explanation), | ||
} | ||
|
||
fn main() {} |
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,25 @@ | ||
trait Bar { | ||
// This can be overridden | ||
fn default_implementation(&self) -> bool { | ||
true | ||
} | ||
fn required_implementation(&self); | ||
} | ||
|
||
impl Bar for Foo { | ||
fn required_implementation(&self) { | ||
// ... | ||
} | ||
} | ||
|
||
impl Foo { | ||
fn new() -> Self { Foo } | ||
} | ||
|
||
fn main() { | ||
let v = Foo::new(); | ||
v.required_implementation(); | ||
v.default_implementation(); | ||
} | ||
|
||
struct Foo; |
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,10 @@ | ||
// &mut denotes a mutable borrow | ||
fn accepts_borrow(thing: &mut u32) { | ||
*thing += 1 | ||
} | ||
|
||
fn main() { | ||
let mut value = 1; | ||
accepts_borrow(&mut value); | ||
println!("{}", value) | ||
} |
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,8 @@ | ||
fn with_lifetimes<'a>(thing: &'a str) -> &'a str { | ||
thing | ||
} | ||
|
||
fn main() { | ||
let foo = "foo"; | ||
println!("{}", with_lifetimes(foo)) | ||
} |