Skip to content

Instantly share code, notes, and snippets.

View kriogenia's full-sized avatar

Soto Estévez kriogenia

View GitHub Profile
@kriogenia
kriogenia / remove_verified_links.user.js
Last active June 9, 2023 13:14
Removes the Verified links in the Nav and Aside from Twitter
@kriogenia
kriogenia / main.rs
Last active June 2, 2023 10:40
Attempt to define as constant a non-const
const ADDRESSES: Vec<&str> = Vec::from([
"http://validaddress.com",
"http://mainmirror.com",
"http://128.0.0.1:1337"
]);
fn main() {
check_addresses()
}
@kriogenia
kriogenia / main.rs
Last active June 2, 2023 10:36
Global unmodifiable variables with OnceLock
use std::sync::OnceLock;
static ADDRESSES: OnceLock<Vec<&str>> = OnceLock::new();
fn main() {
assert!(ADDRESSES.get().is_none());
let first_attempt = ADDRESSES.set(Vec::from([
"http://validaddress.com",
"http://mainmirror.com",
@kriogenia
kriogenia / main.rs
Last active January 28, 2023 15:45
Behavior of Integer Arithmetics
fn main() {
// Result of the different operations computing 255 + 1 (overflowing the u8)
assert_eq!(u8::MAX.wrapping_add(1), 0);
assert_eq!(u8::MAX.checked_add(1), None);
assert_eq!(u8::MAX.overflowing_add(1), (0, true));
assert_eq!(u8::MAX.saturating_add(1), u8::MAX);
// Wrapping examples
assert_eq!(i32::MAX.wrapping_add(12), i32::MIN + 11);
assert_eq!(0u8.wrapping_sub(1), u8::MAX);
@kriogenia
kriogenia / Cargo.toml
Last active September 22, 2022 15:57
Inheriting Workspace Dependencies
[package]
name = "generify_with_compiler_errors"
version = "0.1.0"
edition = "2021"
authors.workspace = true
[dependencies]
num = { workspace = true, default-features = true }
vector2d.workspace = true
@kriogenia
kriogenia / Cargo.toml
Last active September 22, 2022 15:52
How to set Workspace Dependencies
[... rest of the manifest ]
[workspace.dependencies]
num = { version = "0.4", default-features = false }
vector2d = "2.2"
rand = "0.8.5"
@kriogenia
kriogenia / Cargo.toml
Created September 22, 2022 15:09
Inheriting Workspace Package
[package]
name = "add_trait"
version = "0.1.0"
edition.workspace = true
authors.workspace = true
description = "Dissecting Rust Traits to Learn Their Secrets"
documentation = "https://betterprogramming.pub/dissecting-rust-traits-to-learn-their-secrets-839845d3d71e"
homepage.workspace = true
repository.workspace = true
license.workspace = true
@kriogenia
kriogenia / Cargo.toml
Last active September 22, 2022 15:04
How to set Workspace Package
[package]
name = "sotoestevez_medium"
version = "0.1.0"
[workspace]
members = ["add_trait", "beginning_tips", "generify_with_compiler_errors", "modules", "scoped_threads" ]
[workspace.package]
edition = "2021"
authors = ["Soto Estévez <ricardo@sotoestevez.dev>"]
@kriogenia
kriogenia / main.rs
Created August 18, 2022 14:13
Use &[T] instead of &Vec<T>
fn instead_of_this(chars: &Vec<u8>) -> Option<&u8> {
chars.iter().find(|&&x| x == b"\n"[0])
}
fn do_this(chars: &[u8]) -> Option<&u8> {
chars.iter().find(|&&x| x == b"\n"[0])
}
fn main() {
let vec: Vec<u8> = vec![77, 78, 2];
@kriogenia
kriogenia / main.rs
Created August 11, 2022 18:42
Extra example of scoped threads
let mut a = vec![1, 2, 3];
let mut x = 0;
std::thread::scope(|s| {
s.spawn(|| {
println!("hello from the first scoped thread");
// We can borrow `a` here.
dbg!(&a);
});
s.spawn(|| {