From 71cd3d73a117c8acd5fe988b77218940f5652ac4 Mon Sep 17 00:00:00 2001 From: Andrew Hobden Date: Thu, 11 May 2017 13:29:41 +0200 Subject: [PATCH] Add english Syntax chapter --- presentation/chapters/en-US/syntax.chapter | 100 ++++++++++++++++++ presentation/chapters/shared/code/syntax/1.rs | 17 +++ .../chapters/shared/code/syntax/10.rs | 10 ++ .../chapters/shared/code/syntax/11.rs | 15 +++ .../chapters/shared/code/syntax/12.rs | 21 ++++ .../chapters/shared/code/syntax/13.rs | 14 +++ .../chapters/shared/code/syntax/14.rs | 8 ++ presentation/chapters/shared/code/syntax/2.rs | 13 +++ presentation/chapters/shared/code/syntax/3.rs | 14 +++ presentation/chapters/shared/code/syntax/4.rs | 15 +++ presentation/chapters/shared/code/syntax/5.rs | 12 +++ presentation/chapters/shared/code/syntax/6.rs | 16 +++ presentation/chapters/shared/code/syntax/7.rs | 25 +++++ presentation/chapters/shared/code/syntax/8.rs | 10 ++ presentation/chapters/shared/code/syntax/9.rs | 8 ++ presentation/toc/english.html | 1 + presentation/toc/english.md | 1 + 17 files changed, 300 insertions(+) create mode 100644 presentation/chapters/en-US/syntax.chapter create mode 100644 presentation/chapters/shared/code/syntax/1.rs create mode 100644 presentation/chapters/shared/code/syntax/10.rs create mode 100644 presentation/chapters/shared/code/syntax/11.rs create mode 100644 presentation/chapters/shared/code/syntax/12.rs create mode 100644 presentation/chapters/shared/code/syntax/13.rs create mode 100644 presentation/chapters/shared/code/syntax/14.rs create mode 100644 presentation/chapters/shared/code/syntax/2.rs create mode 100644 presentation/chapters/shared/code/syntax/3.rs create mode 100644 presentation/chapters/shared/code/syntax/4.rs create mode 100644 presentation/chapters/shared/code/syntax/5.rs create mode 100644 presentation/chapters/shared/code/syntax/6.rs create mode 100644 presentation/chapters/shared/code/syntax/7.rs create mode 100644 presentation/chapters/shared/code/syntax/8.rs create mode 100644 presentation/chapters/shared/code/syntax/9.rs diff --git a/presentation/chapters/en-US/syntax.chapter b/presentation/chapters/en-US/syntax.chapter new file mode 100644 index 0000000..eea52fb --- /dev/null +++ b/presentation/chapters/en-US/syntax.chapter @@ -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 + +
+ +--- + +## `if` and `if let` + +
+ +--- + +## `match` + +
+ +--- + +## `loop` and `while` + +
+ +--- + +## `for` and `while let` + +
+ +--- + +## `struct`, `type`, and `enum` + +
+ +--- + +## `impl` and `trait` + +
+ +--- + +## Borrowing + +
+ +--- + +## Lifetimes + +`'foo` can be used to describe the lifetime of borrows. + +
+ +--- + +## Scopes + +Rust is block scoped. Scopes can return values. + +
+ +--- + +## Closures + +
+ +--- + +## Generics + +
+ +Generally the `where` syntax is preferred. + +--- + +## `use` and `mod` + +
+ +--- + +## On Semicolons + +A line without a semi-colon implictly returns. Don't worry, the compiler will tell you if you forget it. + +
\ No newline at end of file diff --git a/presentation/chapters/shared/code/syntax/1.rs b/presentation/chapters/shared/code/syntax/1.rs new file mode 100644 index 0000000..9404fa8 --- /dev/null +++ b/presentation/chapters/shared/code/syntax/1.rs @@ -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) +} \ No newline at end of file diff --git a/presentation/chapters/shared/code/syntax/10.rs b/presentation/chapters/shared/code/syntax/10.rs new file mode 100644 index 0000000..3f9fc3d --- /dev/null +++ b/presentation/chapters/shared/code/syntax/10.rs @@ -0,0 +1,10 @@ +fn main() { + let foo = 1; + let bar = { + // Shadows earlier declaration. + let foo = 2; + foo + }; + println!("{}", foo); + println!("{}", bar); +} \ No newline at end of file diff --git a/presentation/chapters/shared/code/syntax/11.rs b/presentation/chapters/shared/code/syntax/11.rs new file mode 100644 index 0000000..d7bbad7 --- /dev/null +++ b/presentation/chapters/shared/code/syntax/11.rs @@ -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); +} \ No newline at end of file diff --git a/presentation/chapters/shared/code/syntax/12.rs b/presentation/chapters/shared/code/syntax/12.rs new file mode 100644 index 0000000..96b395e --- /dev/null +++ b/presentation/chapters/shared/code/syntax/12.rs @@ -0,0 +1,21 @@ +// Inline syntax +fn generic_inline>(thing: S) -> S { + thing +} + +// Where syntax +fn generic_where(thing: Stringish) -> Stringish +where Stringish: AsRef { + thing +} + +// Enums too! +struct GenericStruct { + value: A, +} + +fn main() { + let foo = "foo"; + generic_inline(foo); + generic_where(foo); +} \ No newline at end of file diff --git a/presentation/chapters/shared/code/syntax/13.rs b/presentation/chapters/shared/code/syntax/13.rs new file mode 100644 index 0000000..74fd900 --- /dev/null +++ b/presentation/chapters/shared/code/syntax/13.rs @@ -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() +} \ No newline at end of file diff --git a/presentation/chapters/shared/code/syntax/14.rs b/presentation/chapters/shared/code/syntax/14.rs new file mode 100644 index 0000000..2970148 --- /dev/null +++ b/presentation/chapters/shared/code/syntax/14.rs @@ -0,0 +1,8 @@ +fn do_thing() -> bool { + "Forgotten semicolon" + true +} + +fn main() { + do_thing() +} \ No newline at end of file diff --git a/presentation/chapters/shared/code/syntax/2.rs b/presentation/chapters/shared/code/syntax/2.rs new file mode 100644 index 0000000..59a68bb --- /dev/null +++ b/presentation/chapters/shared/code/syntax/2.rs @@ -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 { /* ... */ } +} \ No newline at end of file diff --git a/presentation/chapters/shared/code/syntax/3.rs b/presentation/chapters/shared/code/syntax/3.rs new file mode 100644 index 0000000..5657f56 --- /dev/null +++ b/presentation/chapters/shared/code/syntax/3.rs @@ -0,0 +1,14 @@ +fn main() { + let maybe_value = Some(2); + match maybe_value { + Some(value) if value == 2 => { + // ... + } + Some(value) => { + // ... + }, + None => { + // ... + }, + } +} \ No newline at end of file diff --git a/presentation/chapters/shared/code/syntax/4.rs b/presentation/chapters/shared/code/syntax/4.rs new file mode 100644 index 0000000..04e23d1 --- /dev/null +++ b/presentation/chapters/shared/code/syntax/4.rs @@ -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; + // ... + } +} \ No newline at end of file diff --git a/presentation/chapters/shared/code/syntax/5.rs b/presentation/chapters/shared/code/syntax/5.rs new file mode 100644 index 0000000..7e01a38 --- /dev/null +++ b/presentation/chapters/shared/code/syntax/5.rs @@ -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() { + // ... + } +} \ No newline at end of file diff --git a/presentation/chapters/shared/code/syntax/6.rs b/presentation/chapters/shared/code/syntax/6.rs new file mode 100644 index 0000000..210b9ea --- /dev/null +++ b/presentation/chapters/shared/code/syntax/6.rs @@ -0,0 +1,16 @@ +struct Empty; + +struct WithFields { + foo: i32, + bar: Choice, +} + +type Explanation = String; + +enum Choice { + Yes, + No, + Maybe(Explanation), +} + +fn main() {} \ No newline at end of file diff --git a/presentation/chapters/shared/code/syntax/7.rs b/presentation/chapters/shared/code/syntax/7.rs new file mode 100644 index 0000000..0cc9f67 --- /dev/null +++ b/presentation/chapters/shared/code/syntax/7.rs @@ -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; \ No newline at end of file diff --git a/presentation/chapters/shared/code/syntax/8.rs b/presentation/chapters/shared/code/syntax/8.rs new file mode 100644 index 0000000..cf5e4cf --- /dev/null +++ b/presentation/chapters/shared/code/syntax/8.rs @@ -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) +} \ No newline at end of file diff --git a/presentation/chapters/shared/code/syntax/9.rs b/presentation/chapters/shared/code/syntax/9.rs new file mode 100644 index 0000000..d5eac82 --- /dev/null +++ b/presentation/chapters/shared/code/syntax/9.rs @@ -0,0 +1,8 @@ +fn with_lifetimes<'a>(thing: &'a str) -> &'a str { + thing +} + +fn main() { + let foo = "foo"; + println!("{}", with_lifetimes(foo)) +} \ No newline at end of file diff --git a/presentation/toc/english.html b/presentation/toc/english.html index 81ccc29..9d4845e 100644 --- a/presentation/toc/english.html +++ b/presentation/toc/english.html @@ -11,6 +11,7 @@
  • Overview

  • Installation
  • +
  • Syntax
  • Mutability
  • Basic types
  • Data structures
  • diff --git a/presentation/toc/english.md b/presentation/toc/english.md index 5f874fd..45f524d 100644 --- a/presentation/toc/english.md +++ b/presentation/toc/english.md @@ -1,6 +1,7 @@ * [Overview](../index.html?chapter=overview&locale=en-US) * [Installation](../index.html?chapter=installation&locale=en-US) +* [Syntax](../index.html?chapter=syntax&locale=en-US) * [Mutability](../index.html?chapter=mutability&locale=en-US) * [Basic types](../index.html?chapter=basic-types&locale=en-US) * [Data structures](../index.html?chapter=data-structures&locale=en-US)