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 test<'a>(f: Box<Fn() -> () + 'a>) { | |
f() | |
} | |
fn main() { | |
let s = "xyz".to_owned(); | |
test(Box::new(|| { | |
println!("blah: {}", s); | |
})); |
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 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!(""); |
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
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> { |
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::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> { |
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
struct A; | |
impl Drop for A { | |
fn drop(&mut self) { | |
println!("dropping A"); | |
} | |
} | |
fn main() { | |
let _ = 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
/* | |
struct Foo { | |
data: Vec<u8> | |
} | |
impl Foo { | |
fn p(&mut self, c: usize) { | |
self.data.insert(0, (c + 42) as u8); | |
} | |
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::ops::Deref; | |
#[derive(Debug)] | |
struct S { | |
i: [u8] | |
} | |
impl S { | |
fn from_bytes(bytes: &[u8]) -> &S { | |
unsafe { &*((bytes as *const [u8]) as *const S)} |
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
// 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) => { |
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 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() { |
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 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!"); | |
}) | |
} |
OlderNewer