Skip to content

Instantly share code, notes, and snippets.

Avatar

Mykhailo Krainik mikhail-krainik

View GitHub Profile
View kata-5848565e273af816fb000449.rs
#![feature(assoc_char_funcs)]
use std::collections::VecDeque;
use std::iter::FromIterator;
fn main() -> Result<(), failure::Error> {
let a = "Hello World";
dbg!(a
.split(" ")
.fold(String::with_capacity(a.len()), |mut acc, w| {
let mut s: VecDeque<char> = w.chars().collect();
@mikhail-krainik
mikhail-krainik / since_the_epoch.rs
Created Jan 15, 2020
Time since the epoch [Rust]
View since_the_epoch.rs
use time;
use std::time::{SystemTime, UNIX_EPOCH};
fn main() {
let a = time::get_time().sec;
dbg!(&a);
let since_the_epoch = SystemTime::now().duration_since(UNIX_EPOCH)
.expect("Time went backwards").as_secs();
println!("{:?}", since_the_epoch);
@mikhail-krainik
mikhail-krainik / vec_result_to_result_vec.rs
Created Oct 2, 2019
Convert Vec<Result<_>> to Result<Vec<_>>
View vec_result_to_result_vec.rs
type CustomError = &'static str;
type Result<T> = std::result::Result<T, CustomError>;
fn main() -> Result<()> {
let val = vec![Ok(1), Ok(2),Err("Some error")];
let res: Vec<i32> = val.into_iter().collect::<Result<_>>()?; // Error: "Some error"
println!("{:?}", res);
Ok(())
@mikhail-krainik
mikhail-krainik / gdbinit
Created Jul 21, 2019 — forked from chrislongo/gdbinit
.gdbinit - A user-friendly gdb configuration file
View gdbinit
# INSTALL INSTRUCTIONS: save as ~/.gdbinit
#
# DESCRIPTION: A user-friendly gdb configuration file.
#
# REVISION : 7.3 (16/04/2010)
#
# CONTRIBUTORS: mammon_, elaine, pusillus, mong, zhang le, l0kit,
# truthix the cyberpunk, fG!, gln
#
# FEEDBACK: https://www.reverse-engineering.net
@mikhail-krainik
mikhail-krainik / rocket_basic_auth.rs
Created May 1, 2019
Rust Rocket Basic Authorization
View rocket_basic_auth.rs
#[macro_use]
extern crate rocket;
use rocket::{Request, Outcome};
pub struct BasicAuth {
pub username: Option<String>,
pub password: Option<String>,
}
impl<'a, 'r> FromRequest<'a, 'r> for BasicAuth {
@mikhail-krainik
mikhail-krainik / playground.rs
Created Apr 10, 2019 — forked from rust-play/playground.rs
Code shared from the Rust Playground
View playground.rs
#![feature(try_trait)]
enum InnerError {
NoneError,
}
trait CustomError: std::error::Error {
fn description(&self) -> &str;
fn cause(&self) -> Option<&std::error::Error> {
None
}
@mikhail-krainik
mikhail-krainik / state_machine.rs
Last active Oct 2, 2019
State Machine in Rust implementation
View state_machine.rs
//# [EN] https://hoverbear.org/2016/10/12/rust-state-machine-pattern/
//# [RU] https://habr.com/ru/post/324382/
fn main() {
// The `<StateA>` is implied here. We don't need to add type annotations!
let in_state_a = StateMachine::new("Blah blah blah".into());
// This is okay here. But later once we've changed state it won't work anymore.
in_state_a.some_unrelated_value;
println!("Starting Value: {}", in_state_a.state.start_value);
View default_params.rs
use std::fmt;
struct ApiUrl<'a> {
host: &'a str,
port: &'a str,
path: &'a str,
}
impl Default for ApiUrl<'_> {
fn default() -> Self {
View rust_vs_crystal
# Crystal cfib.cr
def fib(n)
if n <= 2
1
else
fib(n - 1) + fib(n - 2)
end
end
puts fib(42)
$ crystal build cfib.cr --release
View hyper_example.rs
extern crate hyper;
extern crate futures;
extern crate unicase;
use hyper::Post;
use hyper::header::{Headers, AccessControlAllowOrigin, AccessControlAllowHeaders};
use hyper::server::{Http, Request, Response, Service};
use futures::Stream;
use futures::future::*;
use unicase::Ascii;