Skip to content

Instantly share code, notes, and snippets.

@martimatix
Last active March 16, 2019 11:22
Show Gist options
  • Save martimatix/fd5448103063708763635b048a8ab1f4 to your computer and use it in GitHub Desktop.
Save martimatix/fd5448103063708763635b048a8ab1f4 to your computer and use it in GitHub Desktop.
Rust SIG
[package]
name = "webservice"
version = "0.1.0"
authors = ["Mario Martinez <zenitram.oiram@gmail.com>"]
edition = "2018"
[dependencies]
rocket = "0.4.0"
perfect_numbers = { path = "../perfect-numbers" }

Rust SIG notes

Intro

What is Rust? My short description is that it's a better C or C++. And it's better in the sense that it's safer and has learned a lot from programming languges that have come before it.

We see Rust everywhere. Where can we use it?

  • Terminal programs
  • Embedded software
  • Gaming
  • IoT
  • Web Development
  • Any computationally intensive tasks

Not so good for:

  • Rapid prototying
  • Machine learning

How to install

You could use brew install rust but better to use rustup.rs

rustup is like rvm or nvm but can do so much omre

Today

  • Solve a simple problem
  • Wrap it in a web service

Perfect Numbers

This is a problem for exercism.io

Simple problem but the Option type might take some getting used to.

Explain cargo when running tests.

Version 1

With if/else

Version 2

With ordering - show $ rustup docs

Web Service

$ cargo new webservice

Start with this and install rocket as it takes time.

While installing, discuss:

  • Several web framework for rust - actix web, tower, warp, serverless
  • Talk through source code
use std::cmp::Ordering;
#[derive(Debug, PartialEq, Eq)]
pub enum Classification {
Abundant,
Perfect,
Deficient,
}
pub fn classify(num: u64) -> Option<Classification> {
if num == 0 {
return None;
};
let aliquot_sum: u64 = (1..=(num / 2))
.filter(|potential_factor| num % potential_factor == 0)
.sum();
Some(match aliquot_sum.cmp(&num) {
Ordering::Equal => Classification::Perfect,
Ordering::Greater => Classification::Abundant,
Ordering::Less => Classification::Deficient,
})
}
use std::cmp::Ordering;
#[derive(Debug, PartialEq, Eq)]
pub enum Classification {
Abundant,
Perfect,
Deficient,
}
pub fn classify(num: u64) -> Option<Classification> {
match num {
0 => None,
_ => Some({
let aliquot_sum: u64 = (1..=(num / 2)).filter(|n| num % n == 0).sum();
match aliquot_sum.cmp(&num) {
Ordering::Equal => Classification::Perfect,
Ordering::Greater => Classification::Abundant,
Ordering::Less => Classification::Deficient,
}
}),
}
}
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
extern crate perfect_numbers;
use perfect_numbers::Classification::{Abundant, Deficient, Perfect};
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
#[get("/perfect-number/<number>")]
fn perfect_number(number: u64) -> Option<&'static str> {
perfect_numbers::classify(number).map(|classification| match classification {
Perfect => "Perfect",
Abundant => "Abundant",
Deficient => "Deficient",
})
}
fn main() {
rocket::ignite()
.mount("/", routes![index, perfect_number])
.launch();
}
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
extern crate perfect_numbers;
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
#[get("/perfect-number/<number>")]
fn perfect_number(number: u64) -> &'static str {
perfect_numbers::classify(number)
.map(|classification| match classification {
perfect_numbers::Classification::Perfect => "Perfect",
perfect_numbers::Classification::Abundant => "Abundant",
perfect_numbers::Classification::Deficient => "Deficient",
})
.unwrap()
}
fn main() {
rocket::ignite()
.mount("/", routes![index, perfect_number])
.launch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment