Skip to content
This repository has been archived by the owner on Mar 23, 2020. It is now read-only.

Commit

Permalink
Add english Syntax chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Hobden committed May 11, 2017
1 parent 87756a3 commit 81a888a
Show file tree
Hide file tree
Showing 15 changed files with 298 additions and 0 deletions.
100 changes: 100 additions & 0 deletions presentation/chapters/en-US/syntax.chapter
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>
17 changes: 17 additions & 0 deletions presentation/chapters/shared/code/syntax/1.rs
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)
}
10 changes: 10 additions & 0 deletions presentation/chapters/shared/code/syntax/10.rs
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);
}
15 changes: 15 additions & 0 deletions presentation/chapters/shared/code/syntax/11.rs
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);
}
21 changes: 21 additions & 0 deletions presentation/chapters/shared/code/syntax/12.rs
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);
}
14 changes: 14 additions & 0 deletions presentation/chapters/shared/code/syntax/13.rs
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()
}
8 changes: 8 additions & 0 deletions presentation/chapters/shared/code/syntax/14.rs
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()
}
13 changes: 13 additions & 0 deletions presentation/chapters/shared/code/syntax/2.rs
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 { /* ... */ }
}
14 changes: 14 additions & 0 deletions presentation/chapters/shared/code/syntax/3.rs
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 => {
// ...
},
}
}
15 changes: 15 additions & 0 deletions presentation/chapters/shared/code/syntax/4.rs
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;
// ...
}
}
12 changes: 12 additions & 0 deletions presentation/chapters/shared/code/syntax/5.rs
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() {
// ...
}
}
16 changes: 16 additions & 0 deletions presentation/chapters/shared/code/syntax/6.rs
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() {}
25 changes: 25 additions & 0 deletions presentation/chapters/shared/code/syntax/7.rs
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;
10 changes: 10 additions & 0 deletions presentation/chapters/shared/code/syntax/8.rs
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)
}
8 changes: 8 additions & 0 deletions presentation/chapters/shared/code/syntax/9.rs
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))
}

0 comments on commit 81a888a

Please sign in to comment.