Skip to content

Instantly share code, notes, and snippets.

View segfo's full-sized avatar

segfo segfo

View GitHub Profile
@segfo
segfo / ideal_mixed_ground_meat_calclator.rs
Last active May 4, 2019 09:05
牛肉豚肉合いびき肉計算機初版
#[derive(Clone)]
struct RatioBuilder{
n:i64,
a_name:Option<String>,
a_ratio:Option<i64>,
b_name:Option<String>,
b_ratio:Option<i64>,
}
impl RatioBuilder{
@segfo
segfo / random_string.py
Last active April 7, 2019 13:01
ランダム文字列ワンライナー
l=123;s=string.ascii_letters+string.digits;print(''.join(random.sample((s)*math.ceil(l/(len(s))),k=l)))
@segfo
segfo / main.c
Last active February 28, 2019 13:25
名前付きMutexをつくるだけ。動作確認用。
// C版
// Rust版で作成するMutex名と同じにすると、競合させられる。
#include<windows.h>
#include<stdio.h>
int main() {
// 名前付きミューテックスを作り、確保する
CreateMutexA(NULL,0,"AAAAA_NAMED_MUTEX\0");
if(GetLastError()==183){
// すでに獲得済みのミューテックスの場合は183がGetLastErrorから返却される。
@segfo
segfo / polymorphism.rs
Last active June 4, 2023 18:03
ポリモーフィズムをRustでやってみる
// 参考:https://qiita.com/Nossa/items/a93024e653ff939115c6
trait Shape{
fn area(&self)->f32;
fn whoami(&self)->String;
}
struct Triangle {
pub base:f32,
pub height:f32
}
extern crate crypto_hash;
extern crate ssdeep;
use crypto_hash::{Algorithm, hex_digest};
extern crate levenshtein;
use levenshtein::levenshtein;
fn main() {
// ハッシュ化対象の文字列
let hashes=&[b"give me hashsing!!",b"give you hashsing!",b"AAAAAAAAAAAAAAAAA!"];
// 記録用のベクタ
use std::thread;
use std::time::Duration;
use std::sync::{Arc,Mutex};
use std::ptr::NonNull;
// いんなー
// 32ビット整数値と、外部の配列へのポインタ(C-FFIとかでよくやるやつ)
struct Inner{
num:u32,
u8ptr:SafeRawU8Ptr
@segfo
segfo / random_string.rs
Created April 29, 2018 09:10
Rustでランダム文字列をホイホイ出力
extern crate rand;
use rand::Rng;
fn main() {
let mut vec=(0x20..0x7f)
.filter_map(|n|std::char::from_u32(n as u32))
.collect::<Vec<char>>();
rand::thread_rng().shuffle(&mut vec);
vec.iter().for_each(|n|print!("{}",n));
}
@segfo
segfo / fizzbuzz_odd_double.rs
Last active April 28, 2018 14:57
forを使わずにfizzbuzzしたり、奇数だけを2倍して表示したり。
fn main(){
let n = (0..20).collect::<Vec<u32>>();
let v = n.iter().filter(|n|(*n)%2==1).map(|n|n*2).collect::<Vec<u32>>();
let fb = n.iter().map(fizzbuzz).collect::<Vec<String>>();
println!("{:?}",v);
println!("{:?}",fb);
//もっとスマートに書くと
println!("{:?}",(0..20).filter(|n|(*n)%2==1).map(|n| n * n).collect::<Vec<u32>>());
@segfo
segfo / fuckin_vending_machine.rs
Last active February 10, 2018 04:46
自販機のふるまいを再現してみるプログラム
use std::io::stdin;
use std::io::Write;
use std::str::FromStr;
use std::io::Read;
use std::fmt::{Debug, Display};
use std::error::Error;
#[derive(Debug)]
struct drink<'a>{
price:u32,
@segfo
segfo / fizzbuzz.rs
Created December 31, 2017 05:38
登録された値でFizzbuzzする(登録が面倒なのでマクロも作る)
#[derive(Debug)]
struct FizzBuzzData{
num:u32,
id:&'static str
}
#[derive(Debug)]
struct FizzBuzzN{
data:Vec<FizzBuzzData>,
}