Skip to content

Instantly share code, notes, and snippets.

View eduardonunesp's full-sized avatar
🐚

Eduardo Pereira eduardonunesp

🐚
View GitHub Profile
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();
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
use std::f32;
use std::f64;
let largest: u64 = 0xfffffffffffff;
let zero: u64 = 0x10000000000000;
let num = 1_000_000u64;
let flt = 123.4f64; // Double-like
let fp2 = 0.1f32; // Float-like
let fp3 = 12E+99_f64; //Exponents
use std::env;
use std::path;
use ggez::event::EventHandler;
use ggez::{event, graphics, Context, GameResult};
const GRID_SIZE: (usize, usize) = (10, 10);
const GRID_CELL_SIZE: (usize, usize) = (64, 64);
const SCREEN_SIZE: (f32, f32) = (
GRID_SIZE.0 as f32 * GRID_CELL_SIZE.0 as f32,
type CbFunc = fn();
fn main() {
call_func(print_hello);
call_func(|| println!("{}", "haloo :D")); // Closure
}
fn call_func(f: CbFunc) {
f();
}
#[derive(PartialEq, Debug)]
enum Language {
ENGLISH,
PORTUGUESE,
}
fn select_language(language: Language) -> Language {
match language {
Language::ENGLISH => println!("English selected"),
Language::PORTUGUESE => println!("Portuguese selectionado"),
@eduardonunesp
eduardonunesp / sync_to_async.rs
Created January 12, 2023 15:47
Example of sync function wrapped into a future for the tokio runtime
use std::{thread, time};
use futures::future;
use std::time::Instant;
// Delay function will wait for n seconds
async fn delay(secs: u64) -> anyhow::Result<()> {
let ten_secs = time::Duration::from_secs(secs);
let now = time::Instant::now();
// Wraps a sync call into a future
@eduardonunesp
eduardonunesp / websocket.rs
Created January 5, 2023 00:31
WebSocket RUST
use futures_util::{SinkExt, StreamExt};
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc;
use tokio_tungstenite::{
connect_async,
tungstenite::{Message, Result},
};
use url::Url;
const WS_URL: &'static str = "wss://ws-feed.exchange.coinbase.com";
@eduardonunesp
eduardonunesp / destroy_after_animation_end.cs
Last active September 1, 2022 19:06
Destroy Game Object after animation end
using UnityEngine;
public class ExplosionCenter : MonoBehaviour
{
// Use this for initialization
void Start()
{
Destroy(gameObject, this.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length);
}
@eduardonunesp
eduardonunesp / cpf_checker.rs
Last active August 28, 2022 19:16
CPF Checker
#[derive(Debug, PartialEq)]
enum CPFError {
InvalidDigitAtIndex(u32),
InvalidLength(u32),
Invalid,
}
fn get_dv(cpf: &Vec<u32>, digit_position: u32) -> u32 {
let mut count = digit_position;
let mut sum = 0;