Skip to content

Instantly share code, notes, and snippets.

@rylev
rylev / counter.rs
Created January 26, 2022 17:30
Rust in VS Code Example Code
use std::collections::HashMap;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let path = std::env::args()
.skip(1)
.next()
.ok_or("Must supply name of file as first argument")?;
let contents = std::fs::read_to_string(path)?;
let counter = Counter::new(&contents);
@rylev
rylev / declare.rs
Last active July 24, 2020 15:42 — forked from kennykerr/declare.rs
com::declare! {
// A COM interface declared here
#[uuid(...)]
pub interface IOldSchool {
fn foo();
fn bar();
}
// A COM class declared here (not defined here)
#[uuid(...)]
@rylev
rylev / rust-in-large-organizations-notes.md
Last active February 2, 2023 10:08
Rust in Large Organizations Notes

Rust in Large Organizations

Initially taken by Niko Matsakis and lightly edited by Ryan Levick

Agenda

  • Introductions
  • Cargo inside large build systems
  • FFI
  • Foundations and financial support
@rylev
rylev / windows-crates.rb
Created July 22, 2019 14:48
Top Windows Crates
require "open-uri"
require "json"
PAGE_SIZE=100
NUM_PAGES=1
crates = (1..NUM_PAGES).flat_map do |page|
res = JSON.parse(URI.parse("https://crates.io/api/v1/crates?keyword=windows&page=#{page}&per_page=#{PAGE_SIZE}").read)
res["crates"].map {|c| {name: c["name"], repo: c["repository"]} }
end
@rylev
rylev / proposal.md
Last active April 13, 2019 16:47
File API Proposal

First we have FileList which is simply a list of Files. This is an opaque struct that offers a way to iterate over Files and get individual files by index:

#[derive(Debug, Clone)]
struct FileList { ... }

impl FileList {
  fn get(index: usize) -> Option<File> { ... }

  fn len(&self) -> usize { ... }
@rylev
rylev / learn.md
Created March 5, 2019 10:50
How to Learn Rust

Learning Rust

The following is a list of resources for learning Rust as well as tips and tricks for learning the language faster.

Warning

Rust is not C or C++ so the way your accustomed to do things in those languages might not work in Rust. The best way to learn Rust is to embrace its best practices and see where that takes you.

The generally recommended path is to start by reading the books, and doing small coding exercises until the rules around borrow checking become intuitive. Once this happens, then you can expand to more real world projects. If you find yourself struggling hard with the borrow checker, seek help. It very well could be that you're trying to solve your problem in a way that goes against how Rust wants you to work.

@rylev
rylev / crates.rb
Created March 1, 2019 15:22
Script for downloading the most popular crates and finding how many times they use the unsafe keyword.
require "open-uri"
require "json"
PAGE_SIZE=100
NUM_PAGES=2
crates = []
(1..NUM_PAGES).each do |page|
res = JSON.parse(URI.parse("https://crates.io/api/v1/crates?page=#{page}&per_page=#{PAGE_SIZE}").read)
puts res
crates += res["crates"].map {|c| {name: c["name"], repo: c["repository"]} }
@rylev
rylev / main.rs
Created September 12, 2018 14:56
Bubble Sort In Rust
fn main() {
let mut sortable: [i32; 5] = [5, 8, 2, 7, 6];
let length = sortable.len();
let mut swapped = true;
print_array(&sortable);
while swapped {
swapped = false;
for i in 1..length {
@rylev
rylev / rainbow.a
Created March 21, 2017 09:47
Rainbows on Atari 2600
PROCESSOR 6502; set processor
INCLUDE "vcs.h" ; include helpful stuff
INCLUDE "macro.h" ; include helpful stuff
Seg Code ; set code segment
ORG $F000 ; code should start at 0xF000
BGColor equ $81 ; Reserve space at 0x81
Start
@rylev
rylev / example.ts
Last active March 17, 2017 13:15
TypeScript
function divide(num1: number | undefined, num2: number | undefined): number | 'no!' | undefined {
if (num1 === undefined || num2 === undefined) { return }
// num1 and num2 now have type number
if (num2 === 0) { return 'no!'}
return num1 / num2
}