Skip to content

Instantly share code, notes, and snippets.

View eduardonunesp's full-sized avatar
🐚

Eduardo Pereira eduardonunesp

🐚
View GitHub Profile
let str_to_int = u64::from_str_radix(&"10", 10).expect("Not an integer");
let int_string = "1000000".to_string();
let parse_int: u64 = "1000000".parse().unwrap();
let parse_int: u64 = "1000000".parse::<u64>().unwrap(); // Turbo fish syntax ::<>()
let padded = format!("{:08.2}", 1000.1234); // 00001000.12
let pad_left = format!("{txt:=<width$}", txt="text", width=20); // left padded to a variable place with = as padding
let pad_right = format!("{:=>7}", "text"); // right padded to 7 places with = as padding
let justified = format!("{:=^width$}", "text", 12); // centered with = on both sides
extern crate rand;
use rand::{thread_rng, Rng};
use rand::distributions::range::SampleRange;
use num::{Num, Zero, One};
use std::ops::Add;
// Safely choose a random number
// Use the num crate to add one to the generic number
pub fn safe_range<N>(starting: N, ending: N) -> N where N: Num 6 PartialOrd + Copy + SampleRange {
let end_plus = ending + N::one();
// literals
let byte_data = b"This is a string of bytes";
let raw_string = r##"This "can" have weird \"characters" in it 'no' problem"##;
let raw_byte = br##"you can even mix and match"##;
// Convert to bytes
let data = "abcdefghijklmnopqrstuvwxyz";
let data_ref = data.as_bytes();
let data_owned = data.into_bytes();
extern crate argparse;
use argparse::{ArgumentParser, StoreFalse, StoreTrue, Store};
let mut vart = false;
let mut varf = true;
let mut vars = String::from("");
{
let mut ap = ArgumentParser::new();
ap.set_description("Program description");
ap.refer(&mut vart).add_option(&["-t", "--true"], StoreTrue, "description");
ap.refer(&mut varf).add_option(&["-f", "--false"], StoreFalse, "description");
if cfg!(target_os = "windows") { /* ... */ }
#[cfg(target_os = "linux")] // can be linux android windows macos ios
#[cfg(target_pointer_width = 64)] // target 64 bit systems
#[cfg(target_pointer_width = 32)] // target 32 bit systems
// Compiler features
#![feature(feature1, feature2, feature3)]
#[cfg(feature = "foo")]
// combining multiple conditions
use std::io::{BufReader, BufRead, BufWriter, Write};
use std::fs::File;
use std::prelude::*;
use std::io::{self, stdin, Read};
let sin = io::stdin();
let mut buf = String::new();
let mut name: String = String::new();
println!("Enter something");
let mut f = BufWrtier::new(File::create(filename).expect("Could not create file"));
let text = include_bytes!("filename.txt");
f.write(text);
let text2 = include_str!("filename.txt");
f.write(text2.as_bytes());
let data = vec![b"this will be represented as a vector of bytes"];
f.write(&data[..]);
use std::fs::File;
use std::io::{prelude::*, BufReader, Write};
use std::process::Command;
use actix_multipart::Multipart;
use actix_web::{post, web, App, Error, HttpResponse, HttpServer};
use futures::{StreamExt, TryStreamExt};
use tempdir::TempDir;
#[post("/")]
@eduardonunesp
eduardonunesp / simple_stack.go
Last active July 26, 2021 16:25
Simple Stack
package utils
import "sync"
type Stack struct {
mutex sync.RWMutex
data []interface{}
}
func NewStack() Stack {
@eduardonunesp
eduardonunesp / hamt_container.go
Last active July 21, 2021 20:49
HAMT Container test
package main
import (
"fmt"
"github.com/ipfs/go-cid"
hamt "github.com/ipld/go-ipld-adl-hamt"
ipld "github.com/ipld/go-ipld-prime"
_ "github.com/ipld/go-ipld-prime/codec/dagcbor"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"