Skip to content

Instantly share code, notes, and snippets.

@jpastuszek
jpastuszek / box with lifetime
Last active August 29, 2015 14:27 — forked from anonymous/playground.rs
Shared via Rust Playground
fn test<'a>(f: Box<Fn() -> () + 'a>) {
f()
}
fn main() {
let s = "xyz".to_owned();
test(Box::new(|| {
println!("blah: {}", s);
}));
@jpastuszek
jpastuszek / brake_line.rs
Last active March 3, 2016 17:24 — forked from anonymous/playground.rs
Brake lines iterator
fn main() {
let d = ["A", "B", "C", "D", "E", "F", "G"];
let out = d.iter().enumerate().map(|(i, &l)| ((i + 1) % 3 == 0, l));
for (ln, l) in out {
print!("'{}'", l);
if ln {
println!("");
@jpastuszek
jpastuszek / playground.rs
Created March 4, 2016 15:56 — forked from anonymous/playground.rs
LfIter for &&str iter
const PLF: &'static &'static str = &"\n";
struct LfIter<I> where I: Iterator {
iter: I,
lf_every: u32,
items: u32
}
impl<I> LfIter<I> where I: Iterator {
fn new(iter: I, lf_every: u32) -> LfIter<I> {
@jpastuszek
jpastuszek / playground.rs
Last active March 4, 2016 17:10 — forked from anonymous/playground.rs
Wrap bytes
use std::io::Write;
struct LfIter<I> where I: Iterator {
iter: I,
lf_every: usize,
items: usize
}
impl<I> LfIter<I> where I: Iterator {
fn new(iter: I, lf_every: usize) -> LfIter<I> {
@jpastuszek
jpastuszek / playground.rs
Created June 12, 2016 21:12 — forked from anonymous/playground.rs
Shared via Rust Playground
struct A;
impl Drop for A {
fn drop(&mut self) {
println!("dropping A");
}
}
fn main() {
let _ = A;
@jpastuszek
jpastuszek / playground.rs
Created July 28, 2016 10:07 — forked from anonymous/playground.rs
Shared via Rust Playground
/*
struct Foo {
data: Vec<u8>
}
impl Foo {
fn p(&mut self, c: usize) {
self.data.insert(0, (c + 42) as u8);
}
@jpastuszek
jpastuszek / Rust: Change reference type and Deref back
Last active August 16, 2016 09:22 — forked from anonymous/playground.rs
Shared via Rust Playground
use std::ops::Deref;
#[derive(Debug)]
struct S {
i: [u8]
}
impl S {
fn from_bytes(bytes: &[u8]) -> &S {
unsafe { &*((bytes as *const [u8]) as *const S)}
@jpastuszek
jpastuszek / playground.rs
Created August 25, 2016 15:56 — forked from anonymous/playground.rs
Shared via Rust Playground
// This code is editable and runnable!
fn main() {
let s = "foo\"bar\"baz\"";
//println!("{}", s.split('"').collect::<Vec<_>>().join("\\\""));
let mut iter = s.split('"').peekable();
loop {
match (iter.next(), iter.peek()) {
(Some(i), Some(_)) => print!("{}\\\"", i),
(Some(i), None) => {
@jpastuszek
jpastuszek / playground.rs
Created August 30, 2016 09:59 — forked from anonymous/playground.rs
Rust: title case
fn title_case(s: &str) -> String {
let mut c = s.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().chain(c.flat_map(|t| t.to_lowercase())).collect(),
}
}
fn main() {
@jpastuszek
jpastuszek / playground.rs
Last active September 14, 2016 08:53 — forked from anonymous/playground.rs
Rust: call closure once and only once
fn bar<F, R>(f: F) -> R where F: FnOnce() -> R {
//f(); // used after move
f() // need to get R somehow
}
fn main() {
bar(|| {
println!("foo!");
})
}