Skip to content

Instantly share code, notes, and snippets.

@0atman
Last active November 3, 2020 09:09
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 0atman/fa0295759bd8b25108dc7a8d30166035 to your computer and use it in GitHub Desktop.
Save 0atman/fa0295759bd8b25108dc7a8d30166035 to your computer and use it in GitHub Desktop.
Noodling with rust, with rustic-babel

Rust Research

org-babel

Include crates with :crates '((regex . 0.2)) syntax. However there is a bug in org-babel that only lets you use decimal versions, not semver. Shave off the bugfix suffix. I opened an issue here brotzeit/rustic#176 with a PR to fix here brotzeit/rustic#180

extern crate regex;

use regex::Regex;

fn main() {
    let re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
    println!("{:?}", re.is_match("2014-01-01"));
}
use rayon::prelude::*;
fn sum_of_squares(input: &[i32]) -> i32 {
    input.par_iter().map(|&i| i * i).sum()
}
fn main() {
    println!("{:?}", sum_of_squares(&[10, 20, 30]));
}

Process

data comes back as a byte string, which will look a lot like an array of ints. Cast it with String::from_utf8_lossy().

use std::process::Command;

fn main() {
    let output = Command::new("uname")
        .arg("-a")
        .output()
        .expect("Failed to execute command");

    println!("{:?}", String::from_utf8_lossy(&output.stdout));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment