Skip to content

Instantly share code, notes, and snippets.

View mre's full-sized avatar
🪴
I like plants

Matthias Endler mre

🪴
I like plants
View GitHub Profile
@mre
mre / main.rs
Created March 9, 2018 01:15
A simple clone of `ls -l` in Rust
extern crate chrono;
extern crate libc;
#[macro_use]
extern crate structopt;
use std::fs;
use std::path::PathBuf;
use std::error::Error;
use std::process;
use std::os::unix::fs::PermissionsExt;
@mre
mre / main.c
Created January 2, 2018 20:32
Darwin/macOS copyfile example usage
#include <copyfile.h>
int main()
{
copyfile("test.txt", "copy.txt", 0, COPYFILE_ALL);
}
@mre
mre / each.rb
Created December 16, 2017 16:06
["Ruby", "Rust", "Python", "Cobol"].each do |lang|
puts "Hello #{lang}!"
end
@mre
mre / cat.rs
Created November 6, 2017 22:57
A `cat` implemenation written in Rust using output buffering and stdio locking
#[macro_use]
extern crate error_chain;
use std::env;
use std::fs::File;
use std::io::{self, BufReader, Read, Write};
pub const BUFFER_CAPACITY: usize = 64 * 1024;
error_chain! {
@mre
mre / fib.rs
Created October 27, 2017 21:26
Fibonacci Parallel Rust
extern crate threadpool;
extern crate num_cpus;
use threadpool::ThreadPool;
use std::sync::mpsc::channel;
fn fib_real(number: u64) -> u64 {
match number < 2 {
true => number,
false => run(number - 1) + run(number - 2),
@mre
mre / fizzbuzz.rs
Created October 25, 2017 20:48
Fizzbuzz in Rust
#![feature(generators)]
#![feature(generator_trait)]
use std::ops::{Generator, GeneratorState};
fn fizzbuzz_println(upto: u64) {
for i in 0..upto {
if i % 3 == 0 && i % 5 == 0 {
println!("FizzBuzz");
} else if i % 3 == 0 {
@mre
mre / main.rs
Created October 19, 2017 16:33
Convert String to Option<f64>
fn main() {
let test: String = "0.123".into();
let out = test.parse::<f64>().ok(); // parse test into Some(0.123) here
// my failed attempt:
// time_index: Some(fields[3].parse::<f64>().unwrap_or_default()),
let out = test.parse::<f64>().ok(); // parse test into Some(0.123) here
assert_eq!(out, Some(0.123));
}
@mre
mre / main.rs
Created October 14, 2017 16:13
Print all words containing "rust"
use std::fs::File;
use std::io::{BufReader, BufRead};
fn main() {
let path = "/usr/share/dict/words";
let f = File::open(path).expect("Can't open file");
let buffered = BufReader::new(f);
for line_result in buffered.lines() {
let line = line_result.unwrap();
@mre
mre / yes.rs
Last active October 9, 2017 21:40
The yes unix command written in Rust
use std::env;
use std::io::BufWriter;
use std::io::{self, Write};
const BUFSIZE: usize = 8192;
fn main() {
let expletive = env::args().nth(1).unwrap_or("y".into());
let mut writer = BufWriter::with_capacity(BUFSIZE, io::stdout());
loop {
@mre
mre / quicksort.rs
Last active October 5, 2017 18:31
Functional style quicksort in Rustlang
fn quicksort<E: PartialOrd + Clone>(list: &[E]) -> Vec<E> {
let pivot = match list.get(0) {
None => return vec![],
Some(v) => v,
};
let less: Vec<_> = list[1..].iter().filter(|&e| e <= pivot).cloned().collect();
let more: Vec<_> = list.iter().filter(|&e| e > pivot).cloned().collect();
[quicksort(&less), vec![pivot.clone()], quicksort(&more)].concat()
}