summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2019-01-22 22:39:02 +0000
committerDaniel Silverstone <dsilvers@digital-scurf.org>2019-01-22 22:39:02 +0000
commit2117a095a12ed7f97036767ed59a17c5573f9a7a (patch)
treeb92afdd03839f4ce2be3165bd6b62b91a9e41663
parent86c4fc54c118e2cd8b806cbf3669f7e10d580145 (diff)
downloadrust-talk-2117a095a12ed7f97036767ed59a17c5573f9a7a.tar.bz2
More notes
-rw-r--r--presentation.md254
1 files changed, 231 insertions, 23 deletions
diff --git a/presentation.md b/presentation.md
index 196505f..ae1ba38 100644
--- a/presentation.md
+++ b/presentation.md
@@ -1290,13 +1290,6 @@ fn sum<S>(self) -> S
{ ... }
...
}
-//---
-impl Sum for i32 {
- fn sum<I: Iterator<Item=i32>>(iter: I) -> i32 {
- iter.fold(0, Add::add)
- }
-}
-
```
]
@@ -1311,36 +1304,251 @@ pub trait Sum<A = Self> {
}
```
+].col-12[
+
+```rust
+impl Sum for i32 {
+ fn sum<I: Iterator<Item=i32>>(iter: I) -> i32 {
+ iter.fold(0, Add::add)
+ }
+}
+```
+
]
+???
+
+- The `where` guards allow for traits to define some methods which only exist
+ for suitable subsets of implementors of the trait.
+- In this instance, the `.sum()` method on the `Iterator` trait only exists
+ should there be an implementation of `Sum` for the type which matches the
+ type of the iterator's `Item`.
+
---
-# And more...
+## Meta programming, or IOW, macros
-## 12-column grid layout
+- You've seen them already
-Use to the included **grid layout** classes to split content easily:
-.col-6[
+???
-### Left column
+- Rust macros have a postfix exclamation mark, you've seen some already
+ such as `println!()`
-- I'm on the left
-- It's neat!
- ]
+--
-.col-6[
+- e.g. `println!()`
-### Right column
+???
-- I'm on the right
-- I love it!
+- Yes, println (and print, and writeln, and write, etc) are all macros. They
+ are provided by the standard library
-]
+--
-## Learn the tricks
+- e.g. `include_file!()` or `module_path!()`
-See the [wiki](https://github.com/gnab/remark/wiki) to learn more of what you can do with .alt[Remark.js]
+???
-```
+- Some macros are built into the compiler, such as ones which include files
+ for compilation, or turn bits of the code on and off based on configuration
+ variables
+
+--
+
+- e.g. `serde::Deserialize`
+
+???
+
+- Some macros are what is called 'proc-macros' or 'procedural macros' and they
+ are a special form of Rust library which can be used by the compiler to generate
+ code during compilation. This is fairly new and is still being worked on,
+ but there are some super-powerful features implemented in this already
+
+---
+
+## Procedural macros?
+
+```rust
+#[derive(serde::Deserialize)]
+struct DataLump {
+ who: String,
+ age: Option<u32>,
+}
+
+fn main() -> Result<(), Box<std::error::Error>> {
+ let input = include_str!("input.json");
+ let input: Vec<DataLump> = serde_json::from_str(&input)?;
+ for lump in &input {
+ println!("{} is {}", lump.who,
+ lump.age.map(|n| format!("{} years old", n))
+ .unwrap_or_else(|| "of unknown age".to_owned()));
+ }
+ Ok(())
+}
```
+
+???
+
+- The Rust compiler can derive some implementations of traits automatically
+ for you, such as `Debug` or `PartialEq`/`Eq`
+- But you can have complex traits such as `serde::Deserialize` where a there
+ is a procedural macro in the library which the compiler can use to generate
+ the code for that trait implementation.
+
+_Talk through the code_
+
+---
+
+# The Rust software ecosystem
+
+???
+
+- As much as we might like to think so, programming does not exist in a perfect
+ vacuum where all we have is our one code file and our editor.
+- Programs, libraries, etc, comprise multiple files, they rely on each other
+- They need distribution, aggregation, indexing, etc.
+- Programs need formatting, they need documenting, they need checking
+- The Rust ecosystem attempts to standardise as much as possible of all this
+
+--
+
+- `rustc` and `cargo`
+
+???
+
+- At the base of this pyramid is the rust compiler `rustc` which defines at least
+ some of how a program goes together. It defines the layout of files
+ in the source directory, how modules go together, how things have to be named
+ in order that the compiler can find them.
+- The fundamental unit of compilation for Rust is called the 'crate' and Cargo
+ is the tool which manages them.
+- Cargo defines the next level of structure, providing
+ a standardised tooling interface and a standard layout for libraries and binaries
+ along with tooling to acquire your dependencies, and to build everything and
+ rebuild when needed.
+
+--
+
+- `crates.io`
+
+???
+
+- Since there needs to be somewhere for all those libraries to live and be discoverable,
+ like npm for Node modules, or pypy for Python, Rust has `crates.io`
+- It provides an index of all the crates, known to the ecosystem, and at what versions.
+- Cargo talks to crates.io to acquire "known good" copies of the crate sources
+ which your crate or your dependencies need in order to build
+
+--
+
+- `clippy`
+
+???
+
+- If you thought a normal C compiler was a bit grumpy
+- If you thought a C compiler in `-Wall -pedantic` was a bit rude to you
+- If you thought the Rust compiler could get picky with your nits
+- You should try `clippy` - it is an additional level of linting which looks
+ for antipatterns in your code and teaches you how to do things better or
+ in some cases simply correctly.
+- For example, it'll encourage you to use `.is_empty()` instead of `.len() == 0`
+
+--
+
+- `rustfmt` (and `cargo fmt`)
+
+???
+
+- Consistent code layout reduces cognitive load on consumers of source code
+- Rust defines a standard style (though yes it is configurable)
+- Tooling exists to help you enforce that style
+- Editors often support format-on-save to help you keep to the style
+
+--
+
+- `rustdoc` (and `cargo doc`)
+
+???
+
+- Code, particularly library code, is not considered complete unless it is
+ properly and completely documented. In Rust it's bad if public types,
+ methods, fields, etc. lack documentation
+- the `rustdoc` format is basically markdown and in fact, similarly to Python,
+ code documentation is syntactically part of the language.
+
+--
+
+- ... culminating in `https://docs.rs/`
+
+???
+
+- The combination of `crates.io` `cargo` and `rustdoc` culiminates in the
+ existence of `docs.rs` which is a site which automatically builds and publishes
+ the documentation for every crate on `crates.io`
+
+--
+
+- `#[cfg(test)]`, `#[test]`, `tests/*.rs`, and `cargo test`
+
+???
+
+- Code, particularly library code, **DOES NOT WORK** if it's not tested.
+- Rust offers many ways to write tests. One really nice way is that you can
+ embed code sections into your `rustdoc` and they will be automatically compiled
+ and run to ensure that your code documentation works.
+- Another way is that the compiler has a test configuration which affects visibility
+ of symbols and lets you write test functions which get aggregated and run for you
+- Finally `cargo` jumps in and adds a whole layer of integration tests for you
+ to use as well, in the `tests/` directory. These get automatically linked to
+ your crate for testing, along with test-dependencies managed independently of
+ execution dependencies.
+
+---
+
+# The Rust community
+
+- Reddit `/r/rust`
+- Discord `https://discord.gg/rust-lang`
+- IRC (Mozilla IRC network, `#rust`, `#rust-beginners` etc.)
+- Various Gitter channels (e.g. `#diesel-rs/diesel` for the de-facto database layer)
+- The users forum `https://users.rust-lang.org/`
+- The internals forum `https://internals.rust-lang.org/`
+- All the `https://gitlab.com/rust-lang/` repositories
+- All the `https://gitlab.com/rust-lang-nursery/` repositories
+- Even `#rust` on Codethink IRC 😁
+
+<!-- -->
+
+### `https://www.rust-lang.org/policies/code-of-conduct`
+
+???
+
+- There are many many places to find news on, and information about Rust
+- All governed by a good code of conduct
+- Very welcoming environment in my experience. Comparable in quality only with
+ the `#haskell` people who were always wonderful when I hung out there.
+
+---
+
+class: impact
+
+# Any questions?
+
+???
+
+If people want, I can give a brief walk through of some code, perhaps live-code
+something, otherwise questions, or I'm done.
+
+---
+
+class: impact
+
+# One final point
+
+???
+
+Talk about offering opportunity for Rust tutoring, request people contact in
+private to express interest, explain that the first group will be experimental
+but will offer additional runs later as well.