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
// cf. https://doc.rust-lang.org/reference/expressions/method-call-expr.html | |
trait Or { | |
fn f(self); | |
} | |
struct T; | |
impl Or for &T { | |
fn f(self) { |
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
#[derive(Debug)] | |
struct Holder<T: PrimitiveType>(T::Native); | |
#[derive(Debug)] | |
struct Float64 {} | |
#[derive(Debug)] | |
struct Int64 {} | |
#[derive(Debug)] | |
struct Int32 {} |
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
// [package] | |
// name = "async-internal-spawn" | |
// version = "0.1.0" | |
// authors = ["Ryan James Spencer <spencer.ryanjames@gmail.com>"] | |
// edition = "2018" | |
// [dependencies.async-std] | |
// version = '1.6.2' | |
// features = ['unstable'] |
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
#!/bin/sh -eu | |
export CARGO_HOME="/usr/local/cargo" | |
export RUSTUP_HOME="/usr/local/rustup" | |
export PATH="$CARGO_HOME/bin:$PATH" | |
curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain none -y && rustup toolchain install nightly --profile minimal |
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
#[derive(Debug)] | |
struct X(pub i32); | |
impl AsRef<X> for X { | |
fn as_ref(&self) -> &X { | |
self | |
} | |
} | |
fn f<A: AsRef<X> + std::fmt::Debug>(x: A) { |
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
fn main() { | |
let x = String::from("foo"); | |
// this does not work because it uses the special `'static` lifetime. | |
// which means _the entire program_. The main thread could finish and this | |
// thread could still be running! In that case the reference to x would be invalid. | |
// we can make this work by passing `move` in front of the closure, | |
// effectively copying `x` into the closure's environment. | |
// let normal = std::thread::spawn(|| dbg!(&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
use std::{ | |
fs::File, | |
io::{BufReader, Cursor, Result}, | |
path::Path, | |
}; | |
trait Open<T> { | |
fn open(&self) -> Result<T>; | |
} |
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
fn sine_wave_oscillator (w : f32) -> impl Iterator<Item=f32> { | |
let mut x0 = w.sin(); | |
let mut x1 = 0.0; | |
let b = 2.0 * w.cos(); | |
std::iter::repeat_with(move || { | |
let xn = b * x0 - x1; | |
x1 = x0; | |
x0 = xn; | |
xn |
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
/* | |
If you look, I've also included a pattern to pass the number of times you want | |
the benchmark to run. I default to ten runs instead of one-hundred here as code | |
under inspection may take a long time to run under one-hundred times. You will | |
notice that I'm using nanoseconds for everything, which lets me compute a | |
proper mean average without rounding. | |
*/ | |
macro_rules! bench { | |
($val:expr) => { | |
{ |
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
fn main() { | |
let mut xs = vec![1]; | |
xs[0] = 42; | |
let ys = xs.clone(); | |
let snapshot = move || ys; | |
xs[0] = 100; | |
let reset = snapshot(); | |
assert_eq!(&reset, &vec![42]); | |
assert_eq!(xs, vec![100]); | |
xs = reset; |