Skip to content

Instantly share code, notes, and snippets.

View dns2utf8's full-sized avatar

Stefan Schindler dns2utf8

View GitHub Profile
#[macro_use] extern crate lazy_static; // 1.1.0
extern crate threadpool; // 1.7.1
use std::sync::Mutex;
use threadpool::ThreadPool;
lazy_static! {
pub static ref TABLES_LOAD_POOL: Mutex<ThreadPool> = Mutex::new(setup_thread_pool());
}
/// source: https://gist.github.com/dns2utf8/40da54b578fc5b8cc45662696c18b450
fn main() {
print(42);
print(0);
}
fn print(num: i64) {
println!("normal display: {:x}", num);
println!("padded display: {:04x}", num);
extern crate rand;
fn main() {
use rand::distributions::{Gamma, IndependentSample};
let gamma = Gamma::new(2.0, 1.0);
let mut hist = vec![0; 3];
for _ in 0..102400 {
let v = gamma.ind_sample(&mut rand::thread_rng());
/// source: https://gist.github.com/dns2utf8/a33f344799a7381829ba7295c6258f1f
/// tested with rust 1.20
#[macro_use] extern crate lazy_static;
use std::sync::Mutex;
lazy_static! {
static ref MUTEX: Mutex<usize> = Mutex::new(0);
}
/// source: https://gist.github.com/dns2utf8/c0ad33ad7026f4f101d91099540cb2c6
fn main() {
let something = ("Hello, world!", 42);
println!("normal display: {:#?}", something);
println!("alternative display: {:?}", something);
}
/// Source: https://gist.github.com/dns2utf8/4fdbe0c0df4506e3cd8149e583e8dec2
fn main() {
let (width, height) = (20, 15);
for (x, y) in (0..width).flat_map(|x| (0..height).map(move |y| (x,y))) {
println!("({}/{}) -> {}", x, y, x*y);
}
}
// Source: https://gist.github.com/dns2utf8/130c5c39ca1169ddd3b829ff8ad639c7
// Based upon https://github.com/dns2utf8/drop_guard
extern crate threadpool;
use drop_guard::DropGuard;
use threadpool::ThreadPool;
fn main() {
let pool = ThreadPool::new(4);
//! Source: https://gist.github.com/dns2utf8/828d338bf3cb4214a8127786d89fe085
use std::thread;
use std::sync::mpsc::channel;
use std::sync::Arc;
fn main() {
let (tx, rx) = channel();
// Toogle this line to resolve the error
/// Source: https://gist.github.com/dns2utf8/4e19cc5120094fc29375f7f847eea3f0
fn main() {
round_demo(3, 2);
round_demo(5, 2);
}
fn round_demo(a: i32, b: i32) {
println!("\n calculating rounds for {} over {}", a, b);
@dns2utf8
dns2utf8 / boxed_buffer.rs
Created March 27, 2017 18:25
Boxed buffer
// source: https://gist.github.com/dns2utf8/6d2a7c53414e832be6d9e7a244c395fb
const BUF_SIZE: usize = 8 * 1024 * 1024;
fn main() {
println!("Hello, world!");
let mut buf = Box::new( [0u8; BUF_SIZE] );
buf[BUF_SIZE -1] = 42;
println!("Bye, world! {} // {}", buf[BUF_SIZE -2], buf[BUF_SIZE -1]);
}