Skip to content

Instantly share code, notes, and snippets.

@nebuta
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nebuta/9998319 to your computer and use it in GitHub Desktop.
Save nebuta/9998319 to your computer and use it in GitHub Desktop.
Rust cookbook

Rust Cookbook for Rubyists

These examples compile on rustc 0.11-pre-nightly (540c2a2 2014-04-04 01:01:51 -0700)

Array

// vector testing

// Vector is kind of complicated part of Rust, partly because it's also convolved with pointers ~, &.
// As of Apr 2014, you can assume Vec<T> is a first choice for dynamic (size-changing) arrays.

// https://mail.mozilla.org/pipermail/rust-dev/2014-April/009352.html

use std::vec;

fn main() {
  let vs: Vec<int> = range(0,10).map(|i| i * 10).collect();
  
  println!("{}",vs);
  println!("{}",mk_array());
}

// You don't need to use reference for returning Vec<T>.
// Vec<T> is a small struct with a pointer, so copying is low cost.
fn mk_array() -> Vec<uint> {
  range(0u,10).map(|i| i + 2).collect()
}

Hash (dictionary)

String

Math

IO

File

Loop and iterator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment