This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[..]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("/")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils | |
import "sync" | |
type Stack struct { | |
mutex sync.RWMutex | |
data []interface{} | |
} | |
func NewStack() Stack { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |