Skip to content

Instantly share code, notes, and snippets.

View juliarose's full-sized avatar

Julia juliarose

  • United States
View GitHub Profile
fn main() {
let mut s = String::with_capacity(100);
s.push_str("coffee and 🍌");
s.shrink_to_fit();
println!("`{s}` {} {}", s.len(), s.capacity());
for c in '0'..='🍌' {
if c.is_control() {
type Color = [u8; 3];
/// Blends two colors.
fn blend_colors(a: Color, b: Color, amount: f32) -> String {
a.into_iter().zip(b)
.map(|(a, b)| {
let a = a as f32 * (1.0 - amount);
let b = b as f32 * amount;
let sum = (a + b) as u8;
@juliarose
juliarose / round_to_nearest_divisor.rs
Created February 27, 2023 03:39
Rounds number to the nearest divisor.
use num_traits::cast::AsPrimitive;
/// Rounds number to the nearest divisor.
pub fn round_to_nearest_divisor<T>(num: T, divisor: T) -> T
where
T: AsPrimitive<f32>,
f32: AsPrimitive<T>,
{
let divisor: f32 = divisor.as_();
let num: f32 = num.as_();
@juliarose
juliarose / files.rs
Last active February 13, 2023 18:33
Read/write files in Rust
// https://stackoverflow.com/a/31193386
use std::fs;
use std::io;
fn main() -> Result<(), io::Error> {
// Read a file to a String
let data = fs::read_to_string("/etc/hosts")?;
println!("{}", data);
// Read a file as a Vec<u8>
const getPixels = require("get-pixels");
const fs = require('fs');
const { promisify } = require('util');
const readFile = promisify(fs.readFile);
const path = require('path');
async function getPixelsForImage(imagePath) {
return new Promise((resolve, reject) => {
getPixels(imagePath, (error, pixels) => {
if (error) {
@juliarose
juliarose / next_friday.rs
Last active February 3, 2023 19:50
Next friday at 12pm
use chrono::{Duration, NaiveDate, NaiveDateTime, NaiveTime, Weekday, Datelike};
/// Gets the next day of the week at the given time. If the current date is already the given
/// weekday **and** after the given time, it will return the next one.
fn get_next_day_of_week_at_time(
weekday: Weekday,
at_time: NaiveTime,
) -> Option<NaiveDateTime> {
let mut date = chrono::offset::Local::now();
let one_day = Duration::days(1);
@juliarose
juliarose / retryable.rs
Last active January 16, 2023 04:09
A method for retrying futures which result in any error that implements the trait Retryable to determine whether the request is retryable
use futures_retry::{ErrorHandler, RetryPolicy, FutureRetry};
use rand::Rng;
use std::time::Duration;
#[macro_export]
macro_rules! retry {
( $x:expr, $d:expr, $n:expr ) => {
{
FutureRetry::new(move || $x, RetryHandler::new($d, $n))
.await
fn expensive() -> bool {
println!("expensive");
true
}
fn main() {
// expensive is not executed when previous statement is false
if false && expensive() {
println!("Hello, world!");
}
@juliarose
juliarose / table_from_dump.sh
Created July 30, 2022 01:28
Exctract a single table out of a mysqldump file.
sed -n -e '/CREATE TABLE.*`table_name`/,/Table structure for table/p' db.sql > table.sql
CREATE TABLE colors (
id serial NOT NULL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
hex VARCHAR(6) NOT NULL
);
INSERT INTO colors
(name, hex)
VALUES
('Hot Pink', 'FF0066'),
('Orange', 'FF9900'),