Skip to content

Instantly share code, notes, and snippets.

View stivenson's full-sized avatar
🏠
Working from home

Stivenson stivenson

🏠
Working from home
View GitHub Profile
@stivenson
stivenson / example-struct-trait-impl.rs
Created July 1, 2019 01:20
Example use of Struct, Trait and impl in rust-lang
struct Man {
name: &'static str,
cellphone: i64,
is_alive: bool,
}
trait Person {
fn new(name: &'static str, cellphone: &i64) -> Self;
fn name(&self) -> &'static str;
fn is_alive(&self) -> &bool;
@stivenson
stivenson / new-trait-to-man-struct.rs
Created July 1, 2019 02:54
Example of new trait to man struct
// other Code ...
trait Travel {
fn new(destination: &'static str) -> Self;
fn go(&self) -> &'static str;
}
impl Travel for Man {
fn new(destination: &'static str) -> Man {
// logic ...
@stivenson
stivenson / example-macro.js
Created July 3, 2019 16:45
Example macro in rust-lang
const VALUES_COP: &'static [i32;4] = &[50000, 20000, 10000, 2000];
// macro called cashier
macro_rules! cashier {
() => { // without arguments
println!("Now is necessary that you enter a quantity.");
};
($($x: expr),+) => { // 1 to n arguments
{
let mut total: u64 = 0;
@stivenson
stivenson / example-basic-closure.rs
Last active July 3, 2019 17:29
Basic example of closure with a heavy logic
let result = |word| {
// Heavy logic //
let fname = "text_to_find.txt";
// Contents of file
let contents = fs::read_to_string(fname).expect("Something went wrong reading the file");
// Search a word into file's content
match contents.find(word) {
Some(v) => v as u64,
None => 0