Skip to content

Instantly share code, notes, and snippets.

extern crate libc;
use std::collections::HashMap;
use std::any::TypeId;
use std::string::ToString;
use std::any::Any;
use std::io::{self, Write};
use libc::{EXIT_SUCCESS, EXIT_FAILURE};
type Result<T> = std::result::Result<T, Box<std::error::Error>>;
@U007D
U007D / hkt_lens.rs
Created November 29, 2017 14:50
Simulated HKT in Rust via type lenses
// Project the generics out into an HList
pub trait Unapply {
type Out: ty::Tm<ty::List<ty::Star>> + HList;
}
// Reapply the type to a fresh set of generics
pub trait Reapply<Args: ty::Tm<ty::List<ty::Star>>>
: Unapply
{
type Out: Unapply<Out = Args>;
@U007D
U007D / Static DI - Generic Traits.rs
Last active November 19, 2017 02:15
Static DI Example in Rust
extern crate libc;
use libc::{EXIT_FAILURE, EXIT_SUCCESS};
use std::io::{self, Write};
#[derive(Debug, PartialEq, PartialOrd, Copy, Clone)]
pub enum Error {
Foo(f64),
Bar,
}
//Index impl
pub fn main() {
let mut my_vec = vec![3, 2, 1];
for _ in 0..my_vec.len() {
for i in 0..my_vec.len() - 1 {
if my_vec[i] > my_vec[i + 1] {
let tmp = my_vec[i];
my_vec[i] = my_vec[i + 1];
my_vec[i + 1] = tmp;
}
@U007D
U007D / Errors.md
Last active October 12, 2017 16:11
Issues with using Errors in Rust

No consistent basis with which to work with Errors

(inconsistencies or issues in bold)

using the Standard Library

  • std::fmt::Error:
    • is a (marker) struct
    • does implement PartialEq (among other traits)
    • does not implement a kind() method
    • does not define ErrorKind variants
[brad@socrates:~]$ systemctl status libvirtd
● libvirtd.service - Libvirt Virtual Machine Management Daemon
Loaded: loaded (/nix/store/4lm6pa4wpxj37xvmd2dgjyma9n3dy9s9-libvirt-3.1.0/lib/systemd/system/libvirtd.service; en
Drop-In: /nix/store/kfz1xpchffc15w1926c3ffkzjqql7z8q-system-units/libvirtd.service.d
└─overrides.conf
Active: active (running) since Sat 2017-09-09 10:18:53 PDT; 1h 1min ago
Docs: man:libvirtd(8)
http://libvirt.org
Main PID: 1359 (.libvirtd-wrapp)
Tasks: 18 (limit: 19660)
//-------------
// Build attept
//-------------
Socrates:vulkano_test bRad$ cargo test
Compiling vulkano_test v0.1.0 (file:///Users/bRad/Development/he/experiments/vulkano_test)
warning: unused `#[macro_use]` import
--> src/lib.rs:8:1
|
8 | #[macro_use] extern crate vulkano;
@U007D
U007D / foo.rs
Created August 24, 2017 23:01
Test code from Graydon Hoare's initial commit to the Rust repository on July 23, 2006
module foo;
syntax rx, html;
use bar.baz.(a,b,c);
type nat = int : positive(*);
type natvec1 = vec[nat];
type natvec2 = vec[int : positive(*)];
pred bounded1(int a, int b, int c) = le(a,b), lt(b,c);
@U007D
U007D / HackerRankRustQuickstartTemplate.rs
Last active August 5, 2017 00:21
HackerRank Rust Quickstart Template
// Enter your code here
use std::str::FromStr;
const ERR_READ: &'static str = "Read Error";
const ERR_INPUT_INVALID: &'static str = "Invalid Input Error";
const ERR_MATRIX_EMPTY: &'static str = "Unexpected Empty Matrix Error";
fn readln() -> String {
let mut buffer = String::new();
std::io::stdin().read_line(&mut buffer).ok().expect(ERR_READ);
@U007D
U007D / HexByteString.rs
Created May 31, 2017 22:18
Consuming String Iterator Example
extern crate owned_chars
use std::str::Chars;
use owned_chars::{OwnedChars, OwnedCharsExt};
...
#[derive(Debug)]
pub struct Iter<'a> {
iter: Chars<'a>,