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
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() { |
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
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; | |
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 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_(); |
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
// 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> |
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
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) { |
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 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); |
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 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 |
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
fn expensive() -> bool { | |
println!("expensive"); | |
true | |
} | |
fn main() { | |
// expensive is not executed when previous statement is false | |
if false && expensive() { | |
println!("Hello, world!"); | |
} |
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
sed -n -e '/CREATE TABLE.*`table_name`/,/Table structure for table/p' db.sql > table.sql |
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
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'), |