Skip to content

Instantly share code, notes, and snippets.

🤦‍♂

Sergey Bugaev bugaevc

🤦‍♂
Block or report user

Report or block bugaevc

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View rust_ex5.rs
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");
}
View rust_ex4.rs
// takes v by (immutable) reference
fn count_occurences(v: &Vec<i32>, val: i32) -> usize {
v.into_iter().filter(|&&x| x == val).count()
}
fn main() {
let v = vec![2, 9, 3, 1, 3, 2, 5, 5, 2];
// borrowing v for the iteration
for &item in &v {
// the first borrow is still active
View rust_ex3.rs
// without borrowing
fn print_sum1(v: Vec<i32>) -> Vec<i32> {
println!("{}", v[0] + v[1]);
// returning v as a means of transferring ownership back
// by the way, there's no need to use "return" if it's the last line
// because Rust is expression-based
v
}
// with borrowing, explicit references
View rust_ex2.rs
fn print_sum(a: i32, b: i32) {
println!("{}", a + b);
// the copied a and b are dropped and deallocated here
}
fn main() {
let a = 35;
let b = 42;
// copy the values and transfer
// ownership over the copies to print_sum:
View rust_ex1.rs
fn print_sum(v: Vec<i32>) {
println!("{}", v[0] + v[1]);
// v is dropped and deallocated here
}
fn main() {
let mut v = Vec::new(); // creating the resource
for i in 1..1000 {
v.push(i);
}
View highlight.js
var highlight = "NAME GOES HERE";
(function() {
"use strict";
var rows = document.getElementsByClassName("st_team");
var found;
for(var i = 1; i < rows.length - 3; i++)
View tornado_google.py
from tornado.httpclient import AsyncHTTPClient
import tornado.gen
import tornado.ioloop
import timeit
@tornado.gen.coroutine
def make_request(index):
client = AsyncHTTPClient()
print(index, 'started')
response = yield client.fetch('http://google.com', raise_error=False)
View prefix_notation.c
#include <stdio.h>
#include <stdlib.h>
void report_error(char *s)
{
printf("syntax error: expected %s\n", s);
}
View area_calculation.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define F(x) (f(x)-g(x))
#define F1(x) (f1(x)-g1(x))
typedef double (*func)(double);
double root(func f, func g, func f1, func g1, double a, double b, double eps1)
You can’t perform that action at this time.