Skip to content

Instantly share code, notes, and snippets.

View thehydroimpulse's full-sized avatar

Daniel Fagnan thehydroimpulse

View GitHub Profile
@wycats
wycats / cargo.md
Last active August 29, 2015 13:57
Cargo Status Update - Week of March 17, 2014

Our major goal this week was cleanup: we're planning on moving our current work to the rust-lang repository in the next week or two, so cleanup was the order of the week.

Command Structure

Because Cargo uses the same design as git (many plumbing commands that are used by a smaller number of high-level porcelain commands), getting a standard way to write commands with limited boilerplate was a high priority to get done before we wrote too many commands. We wrote the first few commands by hand, then extracted out some useful abstractions.

A few weeks ago, I wrote a library named Hammer.rs that allows you to decode command-line flags into a struct.

extern crate hammer;
@bvssvni
bvssvni / gist:9674632
Last active December 23, 2023 22:56
A Rust Chain Macro
//! Chain macro in Rust
//!
//! A typical usage is to avoid nested match expressions.
//!
//! Supports:
//! - Pattern matching
//! - Or expressions (A | B => ...)
//! - Guarded statements (x if <cond> => ...)
//! - Implicit else (requires all arms to return same type)
@brson
brson / gist:9470739
Last active February 15, 2016 04:15
Rust doc hacking cheatsheet

Tips for hacking on Rust docs in the Rust tree

Standalone docs

  • make doc/tutorial.html - build the tutorial
  • make check-stage1-doc-tutorial - test the examples in the tutorial

API docs

  • make doc/std/index.html - build the API docs for std
@bvssvni
bvssvni / gist:9304940
Last active August 29, 2015 13:56
A simple stack based evaluator
//! A simple stack based evaluator.
//! You can modify this code to create your own domain specific language.
//!
//! Terminology:
//! - A 'stack' here is represented as a vector.
//! - The 'top' of the stack means the end of the vector.
//!
//! In this example we will create a language that evaluates:
//!
//! Point3D

Summary

Change the following syntax:

struct Foo<T, U> { ... }
impl<T, U> Trait<T> for Foo<T, U> { ... }
fn foo<T, U>(...) { ... }
#[crate_type = "rlib"];
#[comment = "A doubly-linked list implementation using Rc and Weak"];
use std::rc::{Rc, Weak};
use std::cell::RefCell;
type RcN<T> = RefCell<Node<T>>;
/// An immutable, doubly-linked list. Use RefCell to store mutable values.
pub struct List<T> {
@wycats
wycats / hello.rs
Last active January 3, 2016 00:08
fn main() {
do RoutedServer::serve |config, router| {
config.listen("127.0.0.1", 1337);
router.get("/hello", |_,res| res.write("hello world"));
router.get("/posts/:id", |req,res| {
res.write(format!("post {}", req.params["id"]))
});
}
@depp
depp / gist:8378663
Created January 12, 2014 00:05
Iterate over a string
fn main() {
let mut x = "hello".chars();
while (true) {
match x.next() {
Some('o') => return,
Some(c) => println!("Char: {}", c),
None => return
}
}
}

RFC: Proposal for enhancing macros

Currently we have a powerful macro system, but custom control structures stick out like a sore thumb due to the extra set of delimiters that are required.

Implementing a control structure with macro_rules!

Here is an example of a custom for-like construct with some added functionality: