Skip to content

Instantly share code, notes, and snippets.

View eduardonunesp's full-sized avatar
🐚

Eduardo Pereira eduardonunesp

🐚
View GitHub Profile
@eduardonunesp
eduardonunesp / main.go
Created November 20, 2023 16:08
Some random question
// Given a string, cycle the first letter of each word back one word,
// and cycle the last letter of each word forward one word.
// Example input: "who welld horly"
// Example output: "why hello world"
// Do this example by hand:
// Input: "bes le uoogit"
// Output: ""
fn main() {
println!("Sort numbers ascending");
let mut numbers = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1];
println!("Before: {:?}", numbers);
bubble_sort(&mut numbers);
println!("After: {:?}\n", numbers);
println!("Sort strings alphabetically");
let mut strings = ["beach", "hotel", "airplane", "car", "house", "art"];
println!("Before: {:?}", strings);
@eduardonunesp
eduardonunesp / downcast_ref_error.rs
Created January 12, 2023 17:16
Downcast to `&'static str ` an error returned on join thread
use std::thread;
fn main() {
let numbers: Vec<usize> = vec![];
let t = thread::spawn(move || {
let len = numbers.len();
let sum = numbers.into_iter().sum::<usize>();
sum / len
});
@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 / 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;
#[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"),
type CbFunc = fn();
fn main() {
call_func(print_hello);
call_func(|| println!("{}", "haloo :D")); // Closure
}
fn call_func(f: CbFunc) {
f();
}
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,
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