first commit

This commit is contained in:
J. Fernando Sánchez 2020-12-09 15:31:54 +01:00
commit 3e0dc9f01f
7 changed files with 1848 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
target
.*
build

1712
rust/actix/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

13
rust/actix/Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "actixtest"
version = "0.1.0"
authors = ["J. Fernando Sánchez <balkian@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "1.0.9"
actix-service = "0.4.2"
futures = "0.1.29"
#actix-http = "1.0.0"

View File

@ -0,0 +1,20 @@
use futures::future::Future;
use actix_service::Service;
use actix_web::{web, App};
use actix_web::http::{header::CONTENT_TYPE, HeaderValue};
fn main() {
let app = App::new()
.wrap_fn(|req, srv|
srv.call(req).map(|mut res| {
res.headers_mut().insert(
CONTENT_TYPE, HeaderValue::from_static("text/plain"),
);
res
}))
.route(
"/index.html",
web::get().to(|| "Hello, middleware!"),
);
}

45
rust/actix/src/main.rs Normal file
View File

@ -0,0 +1,45 @@
use futures::future::Future;
use actix_service::{Service};
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
use actix_web::http::{header::CONTENT_TYPE, HeaderValue};
fn greet(req: HttpRequest) -> impl Responder {
let name = req.match_info().get("name").unwrap_or("World");
format!("Hello {}!", &name)
}
fn p404(req: HttpRequest) -> impl Responder {
println!("{:?}", req);
actix_web::HttpResponse::NotFound()
.content_type("text/plain")
.body("Thou shall not pass")
}
fn main() {
HttpServer::new(|| {
App::new()
.route("/", web::get().to(greet))
.route("/{name}", web::get().to(greet))
.default_service(
web::route().to(p404))
.wrap_fn(|req, srv| {
println!("{:?}", req);
srv.call(req).map(|mut res| {
res.headers_mut().insert(
CONTENT_TYPE,
HeaderValue::from_static("0001"),
);
res
})
})
})
.bind("127.0.0.1:8000")
.expect("Can not bind to port 8000")
.run()
.unwrap();
}

29
rust/balance-strings.rs Executable file
View File

@ -0,0 +1,29 @@
#!/bin/env cargo-play
fn balanced(s: &str) -> bool {
let mut buffer: String = String::new();
for c in s.chars() {
// dbg!{&buffer};
match c {
'(' | '[' | '{' => buffer.push(c),
')' | ']' | '}' =>
match (c, buffer.chars().last()) {
(']', Some('[')) | (')', Some('(')) | ('}', Some('{')) => {buffer.pop();},
_ => return false,
},
_ => {},
}
}
buffer.is_empty()
}
fn main() {
dbg! {balanced(")(")};
dbg! {balanced("({)}")};
dbg! {balanced("()")};
dbg! {balanced("{}")};
dbg! {balanced("{()}")};
dbg! {balanced("({})")};
dbg! {balanced("(())()")};
dbg! {balanced("((abc)())")};
dbg! {balanced("((abc)()")};
}

26
rust/implicit-call.rs Normal file
View File

@ -0,0 +1,26 @@
use std::fmt::Debug;
#[derive(Debug)]
struct Prueba1<'a>(&'a str);
#[derive(Debug)]
struct Prueba2(usize);
trait GreetTrait: Debug {
fn greet(&self);
}
impl<T> GreetTrait for T
where T: Debug {
fn greet(&self){
println!("{:?}", self);
}
}
fn main(){
let p1 = Prueba1("hello");
let p2 = Prueba2(5);
p1.greet();
p2.greet();
}