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
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(); |
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
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)?; |
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
macro_rules! read_arr { | |
($r: expr, $sz: expr) => {{ | |
let mut buf = [0; $sz]; | |
$r.read_exact(&mut buf)?; | |
Ok(buf) | |
}}; | |
} |
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::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 |
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::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 { |
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(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) | |
} |
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::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) | |
} | |
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::mem; | |
pub struct List { | |
head: Link, | |
} | |
enum Link { | |
Empty, | |
More(Box<Node>), | |
} |
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 x: u32 = 0; | |
unsafe { | |
let ptr: *mut u32 = &mut x; | |
std::ptr::drop_in_place(ptr); | |
std::ptr::drop_in_place(ptr); | |
} | |
} |
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() { | |
println!("Hello, world!"); | |
} |