Skip to content

Instantly share code, notes, and snippets.

View rust-play's full-sized avatar

The Rust Playground rust-play

View GitHub Profile
@rust-play
rust-play / playground.rs
Created January 24, 2020 14:38
Code shared from the Rust Playground
extern crate num_traits;
fn generic_scalar_product<T>(v: &[T], w: &[T]) -> T
where
// A type can implement multiple traits!
// The + sign here means that we expect T to implement
// both num_traits::Zero and std::ops::Mul
T: num_traits::Zero + std::ops::Mul
{
let length = v.len();
@rust-play
rust-play / playground.rs
Created January 24, 2020 14:33
Code shared from the Rust Playground
macro_rules! read_arr {
($r: expr, $sz: expr) => {{
let mut buf = [0; $sz];
$r.read_exact(&mut buf)?;
Ok(buf)
}};
}
fn f(r: &mut impl std::io::Read) -> std::result::Result<(), Box<dyn std::error::Error>> {
read_arr!(r, 8)?;
@rust-play
rust-play / playground.rs
Created January 24, 2020 14:29
Code shared from the Rust Playground
macro_rules! read_arr {
($r: expr, $sz: expr) => {{
let mut buf = [0; $sz];
$r.read_exact(&mut buf)?;
Ok(buf)
}};
}
@rust-play
rust-play / playground.rs
Created January 24, 2020 14:23
Code shared from the Rust Playground
use std::marker::PhantomData;
// T is the return type of the callback FnMut
// Phantomdata cuz the optional callback is inside an ffi struct
struct Config<T>(PhantomData<T>);
struct ConfigBuilder<T>(PhantomData<T>);
impl<T> ConfigBuilder<T> {
pub fn set_callback<F>(&mut self, cb: F) -> &mut Self
where
@rust-play
rust-play / playground.rs
Created January 24, 2020 14:14
Code shared from the Rust Playground
use std::marker::PhantomData;
// T is the return type of the callback FnMut
// Phantomdata cuz the optional callback is inside an ffi struct
struct Config<T>(PhantomData<T>);
enum UnboundT { }
impl Default for Config<UnboundT> {
fn default() -> Self {
@rust-play
rust-play / playground.rs
Created January 24, 2020 14:07
Code shared from the Rust Playground
#[derive(Copy, Clone)]
pub struct Foo(u32, u32);
pub fn foo(x: Foo) -> Option<Foo> {
Some(x.clone())
}
pub fn bar(x: Foo) -> Option<Foo> {
Some(x)
}
@rust-play
rust-play / playground.rs
Created January 24, 2020 14:04
Code shared from the Rust Playground
use std::marker::PhantomData;
// T is the return type of the callback FnMut
// Phantomdata cuz the optional callback is inside an ffi struct
struct Config<T> (PhantomData<T>);
impl<T> Config<T> {
pub fn new() -> Self {
Config::<T>(PhantomData)
}
@rust-play
rust-play / playground.rs
Created January 24, 2020 13:54
Code shared from the Rust Playground
use std::mem;
pub struct List {
head: Link,
}
enum Link {
Empty,
More(Box<Node>),
}
@rust-play
rust-play / playground.rs
Created January 24, 2020 13:52
Code shared from the Rust Playground
fn main() {
let mut x: u32 = 0;
unsafe {
let ptr: *mut u32 = &mut x;
std::ptr::drop_in_place(ptr);
std::ptr::drop_in_place(ptr);
}
}
@rust-play
rust-play / playground.rs
Created January 24, 2020 13:49
Code shared from the Rust Playground
fn main() {
println!("Hello, world!");
}