Initially taken by Niko Matsakis and lightly edited by Ryan Levick
- Introductions
- Cargo inside large build systems
- FFI
- Foundations and financial support
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); |
com::declare! { | |
// A COM interface declared here | |
#[uuid(...)] | |
pub interface IOldSchool { | |
fn foo(); | |
fn bar(); | |
} | |
// A COM class declared here (not defined here) | |
#[uuid(...)] |
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 |
First we have FileList
which is simply a list of File
s. 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 { ... }
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.
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"]} } |
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 { |
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 |
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 | |
} |