Skip to content

Instantly share code, notes, and snippets.

View SpeedSX's full-sized avatar

speedsx SpeedSX

  • Kyiv, Ukraine
View GitHub Profile
@SpeedSX
SpeedSX / equal_ignoring_case.rs
Last active November 17, 2020 16:07
Equal / StartsWith Ignoring case
fn equal_ignoring_case(a: &str, b: &str) -> bool {
a.chars().flat_map(char::to_lowercase).eq(b.chars().flat_map(char::to_lowercase))
}
fn starts_with_ignoring_case(s: &str, prefix: &str) -> bool {
let mut s = s.chars().flat_map(char::to_lowercase);
let mut prefix = prefix.chars().flat_map(char::to_lowercase);
while let Some(s_ch) = s.next() {
match prefix.next() {
Some(p_ch) => if s_ch != p_ch {
@SpeedSX
SpeedSX / read_env_var_with_default.rs
Created October 24, 2020 10:11
Read env var with default value
env::var("RABBIT_MQ_PORT").ok().and_then(|x| x.parse::<u32>().ok()).unwrap_or(5672)
@SpeedSX
SpeedSX / habr-swift-vs-rust.rs
Created July 18, 2020 20:05 — forked from frol/habr-swift-vs-rust.rs
My refactored version of Rust implementation to the article "Swift против Rust — бенчмаркинг на Linux с (не)понятным финалом" https://habr.com/en/post/450512/
//[dependencies]
//serde_json = "1.0"
use serde_json::Value;
use std::collections::{HashMap, HashSet};
const FILE_BUFFER_SIZE: usize = 50000;
//source data
#[derive(Default)]
@SpeedSX
SpeedSX / string_concat.rs
Created March 4, 2020 09:01
Concat two strings in Rust
#[test]
fn plus() {
let a = String::from("a");
let b = String::from("b");
assert_eq!(a + &b, "ab");
}
#[test]
fn plus_assign() {
fn main() {
let points = vec![
Point { x: (-1.0, 1.0), y: 1.0 },
Point { x: (0.0, -1.0), y: -1.0 },
Point { x: (10.0, 1.0), y: 1.0 }
];
let result = Solution::regress(&points);
println!("result {:#?}", result);
}
@SpeedSX
SpeedSX / DI_framework.rs
Created February 17, 2019 18:19
Example of using macros to implement late binding in Rust
#![feature(box_syntax)]
// Framework
trait Bind<S: ?Sized> {
fn bind(&mut self, service: Box<S>);
fn get(&self) -> &Box<S>;
fn get_mut(&mut self) -> &mut Box<S>;
}
@SpeedSX
SpeedSX / my_atoi.rs
Created February 4, 2019 22:07
String to Integer
impl Solution {
pub fn my_atoi(str: String) -> i32 {
let mut started = false;
let mut result: i32 = 0;
let mut sign: i32 = 1;
for b in str.into_bytes() {
if b == 32 {
if started {
break;
}
@SpeedSX
SpeedSX / is_valid.rs
Created February 3, 2019 19:42
Valid parentheses
impl Solution {
pub fn is_valid(s: String) -> bool {
let opening = "([{";
let closing = ")]}";
let mut par: Vec<usize> = vec![];
for c in s.chars() {
match opening.find(c) {
Some(i) => {
par.push(i);
},
@SpeedSX
SpeedSX / divide.rs
Created February 3, 2019 19:41
Divide integers
use std::iter::FromIterator;
impl Solution {
pub fn divide(dividend: i32, divisor: i32) -> i32 {
let dividend_p = (dividend as i64).abs();
let divisor_p = (divisor as i64).abs();
let div_chars: Vec<char> = dividend_p.to_string().chars().collect();
let mut result_chars = Vec::new();
let mut i: usize = 0;
let mut part_div_chars = Vec::new();
@SpeedSX
SpeedSX / roman_to_int.rs
Last active February 3, 2019 19:40
Roman to integer
fn main() {
let result = Solution::roman_to_int("MCMXCVI");
println!("{}", result);
}
struct Solution {}
impl Solution {
pub fn roman_to_int(s: String) -> i32 {
let states = vec![('-', 'I', 1),