Skip to content

Instantly share code, notes, and snippets.

View bugaevc's full-sized avatar

Sergey Bugaev bugaevc

View GitHub Profile
fn the_longest<'a>(s1: &'a str, s2: &'a str) -> &'a str {
if s1.len() > s2.len() { s1 } else { s2 }
}
fn main() {
let s1 = String::from("Python");
// explicitly borrowing to ensure that
// the borrow lasts longer than s2 exists
let s1_b = &s1;
{
fn search<'a, 'b>(needle: &'a str, haystack: &'b str) -> Option<&'b str> {
// imagine some clever algorithm here
// that returns a slice of the original string
let len = needle.len();
if haystack.chars().nth(0) == needle.chars().nth(0) {
Some(&haystack[..len])
} else if haystack.chars().nth(1) == needle.chars().nth(0) {
Some(&haystack[1..len+1])
} else {
None
fn middle_name<'a>(full_name: &'a str) -> &'a str {
full_name.split_whitespace().nth(1).unwrap()
}
fn main() {
let name = String::from("Harry James Potter");
let res = middle_name(&name);
assert_eq!(res, "James");
// won't compile:
// this does not compile
fn middle_name(full_name: &str) -> &str {
full_name.split_whitespace().nth(1).unwrap()
}
fn main() {
let res;
{
let name = String::from("Harry James Potter");
fn middle_name(full_name: &str) -> &str {
full_name.split_whitespace().nth(1).unwrap()
}
fn main() {
let name = String::from("Harry James Potter");
let res = middle_name(&name);
assert_eq!(res, "James");
}
Mar 05 12:35:43 org.gnome.Shell.desktop[1659]: (EE)
Mar 05 12:35:43 org.gnome.Shell.desktop[1659]: (EE) Backtrace:
Mar 05 12:35:43 org.gnome.Shell.desktop[1659]: (EE) 0: /usr/bin/Xwayland (OsLookupColor+0x139) [0x5909c9]
Mar 05 12:35:43 org.gnome.Shell.desktop[1659]: (EE) 1: /lib64/libpthread.so.0 (__restore_rt+0x0) [0x7ffae328d5bf]
Mar 05 12:35:43 org.gnome.Shell.desktop[1659]: (EE) 2: /usr/bin/Xwayland (os_move_fd+0x2ca0) [0x599b40]
Mar 05 12:35:43 org.gnome.Shell.desktop[1659]: (EE) 3: /usr/bin/Xwayland (dri3_send_open_reply+0x6d) [0x4f123d]
Mar 05 12:35:43 org.gnome.Shell.desktop[1659]: (EE) 4: /usr/bin/Xwayland (InitExtensions+0x657) [0x42ab17]
Mar 05 12:35:43 org.gnome.Shell.desktop[1659]: (EE) 5: /lib64/libffi.so.6 (ffi_call_unix64+0x4c) [0x7ffae282ac58]
Mar 05 12:35:43 org.gnome.Shell.desktop[1659]: (EE) 6: /lib64/libffi.so.6 (ffi_call+0x32a) [0x7ffae282a6ba]
Mar 05 12:35:43 org.gnome.Shell.desktop[1659]: (EE) 7: /lib64/libwayland-client.so.0 (wl_log_set_handler_client+0x1c9e) [0x7ffae506e80e]
@bugaevc
bugaevc / foo.cpp
Created July 8, 2018 13:01
clang rewrite objc
#ifndef __OBJC2__
#define __OBJC2__
#endif
struct objc_selector; struct objc_class;
struct __rw_objc_super {
struct objc_object *object;
struct objc_object *superClass;
__rw_objc_super(struct objc_object *o, struct objc_object *s) : object(o), superClass(s) {}
};
#ifndef _REWRITER_typedef_Protocol
@bugaevc
bugaevc / obozrenie-rs.diff
Created August 16, 2018 09:10
gio-rs resources
diff --git a/src/main.rs b/src/main.rs
index ac542de..e40605d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -16,12 +16,10 @@ use futures_timer::*;
use gio::prelude::*;
use gtk::prelude::*;
use librgs::ServerEntry;
-use static_resources::Resources;
use std::sync::{
@bugaevc
bugaevc / type_name.cpp
Created June 8, 2020 18:07
Get the name of a type as a string, at compile time
#include <cstddef>
#include <iostream>
#include <string_view>
template<typename T>
static constexpr inline const char *helper1() {
// Must have the same signature as helper2().
return __PRETTY_FUNCTION__ + __builtin_strlen(__FUNCTION__);
}
#include <stdio.h>
#include <type_traits>
#include <utility>
#include <new>
template<typename T>
void forget1(T &&value) {
union Forget {
~Forget() { }
typename std::remove_reference<T>::type t;