Skip to content

Instantly share code, notes, and snippets.

@psychoss
psychoss / fast.rs
Created January 13, 2016 06:18 — forked from Dr-Emann/fast.rs
bad rust?
let start = time::now();
let metadata = try!(fs::metadata(&path));
let mut file = try!(File::open(&path));
let mut buf = vec![0u8; metadata.len() as usize];
try!(file.read(&mut buf));
buf.make_ascii_lowercase();
let mut str: &str = try!(str::from_utf8(&buf));
@psychoss
psychoss / header.rs
Created January 13, 2016 06:19 — forked from m4rw3r/header.rs
http.rs: Simple HTTP-library for Rust
use std::fmt;
use std::ascii::StrAsciiExt;
#[deriving(Clone,Eq)]
pub enum HeaderName {
Accept,
AcceptCharset,
AcceptEncoding,
AcceptLanguage,
Age,
@psychoss
psychoss / README.md
Created January 13, 2016 08:44 — forked from mbostock/.block
The Gist to Clone All Gists

Run like so:

chmod u+x gist-clone-all
./gist-clone-all username OAUTH_TOKEN

You'll want to replace “username” with your own username and “OAUTH_TOKEN” with an OAuth token.

This script clones using the push URL, so you should probably be the owner of the gists. You could also use this to clone someone else's gists, but in that case you may wish to edit the code to use gist_pull_url instead.

@psychoss
psychoss / pad-left-with-zeros.rs
Last active January 15, 2016 12:53
Some tricks about the Rust
fn main() {
// Pads string with zeros on the left side if it’s shorter than 3.
println!("{:0>3}",0);
}
@psychoss
psychoss / file-dealer.rs
Last active November 13, 2017 15:52
Something about deal with file system with Rust
use std::thread;
use std::fs::{self, DirEntry};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::io;
macro_rules! or_return {
($expr:expr) => (match $expr {
::std::result::Result::Ok(val) => val,
::std::result::Result::Err(err) =>{
@psychoss
psychoss / gist-backup.js
Last active January 15, 2016 12:49
GitHub Gists Backup
#!/usr/bin/env node
var rest = require('restler');
var request = require('request');
var fs = require('fs');
var mkdirp = require('mkdirp');
var prompt = require('sync-prompt').prompt;
var ghUsername = process.argv[2] || prompt('Your GitHub Username? ');
var ghPassword = process.argv[3] || prompt.hidden('Your GitHub Password? ');
var savedir = process.argv[4] || prompt('Path to back-up dir (will create if not exists)? ') || 'gists';
@psychoss
psychoss / actor.rs
Last active January 15, 2016 13:02
Rust Concurrency
#![allow(dead_code)]
use std::collections::HashMap;
use std::f32::INFINITY;
use std::sync::mpsc::{channel, Sender, Receiver};
use std::thread;
struct Store {
name: String,
prices: HashMap<String, f32>,
@psychoss
psychoss / bench.txt
Last active January 15, 2016 13:01
Rust Performance
-> % cat src/main.rs
extern crate time;
use std::env;
use std::ptr;
use time::Duration;
const SIZE: usize = 10 * 1024 * 1024;
@psychoss
psychoss / c-regex.c
Last active January 19, 2016 09:21
Deal with file system an C language
/* match: search for regexp anywhere in text */
int match(char *regexp, char *text) {
if (regexp[0] == '^')
return matchhere(regexp + 1, text);
do { /* must look even if string is empty */
if (matchhere(regexp, text))
return 1;
} while (*text++ != '\0');
return 0;
}
@psychoss
psychoss / macros-concat_idents.rs
Last active January 15, 2016 12:48
Something about Rust Macros
macro_rules! test(
($x:ident) => ({
let z = concat_idents!(hello_, $x);
z();
})
)
fn hello_world() { ... }
fn main() {