Skip to content

Instantly share code, notes, and snippets.

@bwindels
Last active April 8, 2016 13:18
Show Gist options
  • Save bwindels/2a0a34f910395761809c2ec675dfd68f to your computer and use it in GitHub Desktop.
Save bwindels/2a0a34f910395761809c2ec675dfd68f to your computer and use it in GitHub Desktop.
Rust snippets
fn main() {
let a = 0b10010001u8;
let b = !a;
println!("{:08b}", a);
println!("{:08b}", b);
}
struct Greeter {
name: String
}
impl Greeter {
pub fn greet(&mut self) {
let slice = self.name.as_str();
println!("hällo {}!", slice);
}
}
fn main() {
let mut name = String::from("Bruno");
name.pop();
name.push_str("hilde");
let mut greeter = Greeter {name: name};
greeter.greet();
}
use std::fmt::Write;
struct CountingPrinter {
counter: u16,
buffer: String
}
fn print_line_to_stdout(text: &str) {
println!("{}", text);
}
fn prepare_buffer(buffer: &mut String, number: u16, text: &str) {
buffer.clear();
write!(buffer, "{}", number).unwrap();
buffer.push_str(": ");
buffer.push_str(text);
}
impl CountingPrinter {
pub fn print(&mut self, text: &str) {
self.counter += 1;
prepare_buffer(&mut self.buffer, self.counter, text);
print_line_to_stdout(self.buffer.as_str());
}
}
fn main() {
let mut printer = CountingPrinter {counter: 0, buffer: String::new()};
let mut name = String::from("Bruno");
{
let borrowed_name = &mut name;
printer.print(borrowed_name.as_str());
borrowed_name.pop();
borrowed_name.push_str("hilde");
printer.print(borrowed_name.as_str());
}
}
enum Orientation {
Horizontal,
Vertical
}
struct Point {
x: u32,
y: u32
}
fn main() {
let o = Orientation::Horizontal;
let n = Point {x: 1, y: 10};
let p = Point {x: 6, y: 2};
let (a, b) = match o {
Orientation::Horizontal => (n.x, p.x),
Orientation::Vertical => (n.y, p.y),
};
println!("a is {} and b is {}", a, b);
}
use std::env;
fn main() {
for (name, value) in env::vars() {
println!("{}: {}", name, value);
}
}
mod geom {
pub struct Point {
pub x: u16,
pub y: u16
}
}
pub mod output {
pub mod screen {
use geom::Point;
pub fn print_at(pos: Point, text: &str) {
println!("{} at {}, {}", text, pos.x, pos.y);
}
}
}
fn main() {
output::screen::print_at(geom::Point {x: 4, y: 6}, "hello world");
}
use std::collections::HashMap;
trait PreferenceReader {
fn read_u32(&self, name: &str) -> Option<u32>;
}
trait Input {
fn read(&self) -> Result<String, &str>;
}
trait Output {
fn write(&mut self, &str);
}
struct App<I, O> {
input: I,
output: O
}
impl<I: Input, O: Output> App<I, O> {
fn run(&mut self) {
let input = self.input.read().unwrap();
let message = format!("received from input: {}", input);
self.output.write(message.as_str());
}
}
struct HashMapPrefReader {
integral_values: HashMap<&'static str, u32>
}
impl PreferenceReader for HashMapPrefReader {
fn read_u32(&self, name: &str) -> Option<u32> {
self.integral_values.get(name).map(|n| *n)
}
}
struct StdoutOutput {a: u32}
impl Output for StdoutOutput {
fn write(&mut self, message: &str) {
self.a += 1;
println!("output ({}): {}", self.a, message);
}
}
struct FixedInput<P> {
fix: String,
preference_reader: P
}
impl<P: PreferenceReader> Input for FixedInput<P> {
fn read(&self) -> Result<String, &str> {
let times = self.preference_reader.read_u32("input_repeat_times");
times.map(|n| (0 .. n).fold(String::with_capacity(n as usize * self.fix.len()),
|mut s, _| {s.push_str( self.fix.as_str()); s }))
.ok_or("could not read pref")
}
}
fn main() {
let mut prefs = HashMap::new();
prefs.insert("input_repeat_times", 5);
let input = FixedInput {
fix: String::from("test"),
preference_reader: HashMapPrefReader {integral_values: prefs}
};
let mut app = App {
input: input,
output: StdoutOutput {a: 5}
};
app.run();
}
use std::fmt::Write;
fn main() {
let mut buffer = String::with_capacity(100);
buffer.push_str("ik wil ");
write!(&mut buffer, "{}", 643u32).unwrap();
buffer.push_str(" bananen");
println!("{}", buffer);
}
use std::time::Duration;
fn main() {
let a = Duration::new(3, 0);
let b = Duration::new(2, 200);
if b > a {
println!("overflow would happen");
}
else {
let c = a - b;
println!("{:?}", c);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment