These are chat archives for rust-lang/rust

10th
Apr 2018
Dylan DPC
@Dylan-DPC
Apr 10 2018 06:19
What's a good crate to build lightweight small (micro) APIs in rust? :P
i've looked at pencil but thinking if there was something more "API-ish"
Christian Witts
@ChristianWitts
Apr 10 2018 06:26
rustless perhaps, haven’t used it personally though
the HTTP Server list has a good selection
https://github.com/rust-unofficial/awesome-rust#web-programming
Dylan DPC
@Dylan-DPC
Apr 10 2018 06:30

Rustless is based on Iron, which is based on Hyper, which is synchronous. Hyper has a lot of limitations right now, and can't handle many simultaneous connections, especially with keep-alive. So it is highly recommended to use light asynchronous web server such as Nginx as a reverse proxy server with Rustless.

is this a major concern though?

oh cool. i was looking at the list from AWWY
Christian Witts
@ChristianWitts
Apr 10 2018 06:32
yeah, it would be a concern to me. just noticed that it was sync only :<
I’m currently trying out actix-web, as I was planning on using actix for something else. would be handy for me to have a “common” framework I use
Dylan DPC
@Dylan-DPC
Apr 10 2018 06:34
ya i was looking at actix-web as well
Christian Witts
@ChristianWitts
Apr 10 2018 06:35
Rocket is pretty cool, nice codegen which reminds me of python decorators so you just write minimal code, but I’m not sure I want to rely on the nightly compiler for it (although it “shouldn’t” be much of an issue)
Dylan DPC
@Dylan-DPC
Apr 10 2018 06:36
yah it is. but this is a small API
Andy Bell
@Andy-Bell
Apr 10 2018 08:34
Nickel worked for me in the past, potential issues with it currently: still on Hyper 0.10 (sync version), still uses rustc_serialize instead of Serde, however with out small API we used it for we did not run into issues with the sync nature of the server, but ymmv
Dylan DPC
@Dylan-DPC
Apr 10 2018 08:49
ok thanks
unreadable
@unreadable
Apr 10 2018 15:14
hey
what's the right way to determine the type of a variable
no matter the type
Michal 'vorner' Vaner
@vorner
Apr 10 2018 15:15
What do you mean? Like as a human programmer you want the compiler to tell you the type? Try to assign it to a variable of the wrong type, like:
let broken: () = the_variable;
It'll fail to compile and tell you that the type isn't () and it'll tell you which one.
apiraino
@apiraino
Apr 10 2018 15:16
the "poorman's way" I adopt sometimes is using println!() and let the "missing Display impl" error say that
unreadable
@unreadable
Apr 10 2018 15:17
I wanna capture the type as well as a string
for example 5 should return i32 and struct Cat should return Cat and so on..
 let broken: () = the_variable;
apiraino
@apiraino
Apr 10 2018 15:18
ok you basically want the "type of"
unreadable
@unreadable
Apr 10 2018 15:18
how can I execute it in a catch block and keep the type in a parsed string
yeah
Dylan DPC
@Dylan-DPC
Apr 10 2018 15:23
also Jetbrains products automatically suggest you the type :P
works well except sometimes for generics
Dylan DPC
@Dylan-DPC
Apr 10 2018 15:23
why do you want the type though?
apiraino
@apiraino
Apr 10 2018 15:23
the discussion looks to not have landed a consensus:
https://github.com/rust-lang/rfcs/issues/1144#issuecomment-298368802
unreadable
@unreadable
Apr 10 2018 15:24
btw
Shouldn't I be able to catch the error in a string?
 let broken: () = Some(5)
will throw an error with the type, right?
if I can catch it, will get the type
but not sure you can "catch" an unexpected type error you're lookin for
Ingvar Stepanyan
@RReverser
Apr 10 2018 15:29

will throw an error with the type, right?

The compilation will fail, so you can't "catch" it as it's not a runtime error.

Dylan DPC
@Dylan-DPC
Apr 10 2018 15:31
weird question:
is there a way i can set a variable from main and access it later in another fn? :P
Ingvar Stepanyan
@RReverser
Apr 10 2018 15:31
Sure, just use global aka static variable
Dylan DPC
@Dylan-DPC
Apr 10 2018 15:32
i can't pass it to the function because the function is called from a "router"
uhmm
but how do i access the variable inside main?
Ingvar Stepanyan
@RReverser
Apr 10 2018 15:32
like any other variable
Dylan DPC
@Dylan-DPC
Apr 10 2018 15:32
since i don't have self
Ingvar Stepanyan
@RReverser
Apr 10 2018 15:32
why do you need self?
Dylan DPC
@Dylan-DPC
Apr 10 2018 15:32
i usually avoid globals :D
oh wait i got your point now
thanks :D
Dylan DPC
@Dylan-DPC
Apr 10 2018 17:49
@RReverser i can't use a static because it isn't a constant (i'm calling a fn that is creating a struct)
Michal 'vorner' Vaner
@vorner
Apr 10 2018 17:51
The lazy_static crate?
Dylan DPC
@Dylan-DPC
Apr 10 2018 17:55
uhmm no this is nothing w.r.t that
Dylan DPC
@Dylan-DPC
Apr 10 2018 18:02
@vorner i need a way to create an object and pass it to a registered route (using actix_web)
Ingvar Stepanyan
@RReverser
Apr 10 2018 18:10
@Dylan-DPC why "nothing"?
lazy-static is exactly for that sort of situations
when you need a heap alloc or function call or whatever
Dylan DPC
@Dylan-DPC
Apr 10 2018 18:13
oops i thought you were asking if i was asking about lazy_static :D
ok will try that
thanks
Dylan DPC
@Dylan-DPC
Apr 10 2018 18:47
any clue why i would get:
error: no rules expected the token `BUCKETS`
  --> src/main.rs:44:12
   |
44 |     static BUCKETS: BucketCollection = { BucketCollection::new(24) }
   |            ^^^^^^^

error: aborting due to previous error
Ingvar Stepanyan
@RReverser
Apr 10 2018 18:47
Did you check lazy static docs on how to use it?
Dylan DPC
@Dylan-DPC
Apr 10 2018 18:47
yep
Ingvar Stepanyan
@RReverser
Apr 10 2018 18:48
lazy_static! {
    static ref HASHMAP: HashMap<u32, &'static str> = {
        let mut m = HashMap::new();
        m.insert(0, "foo");
        m.insert(1, "bar");
        m.insert(2, "baz");
        m
    };
}
this is from their README example
Dylan DPC
@Dylan-DPC
Apr 10 2018 18:48
yep
Ingvar Stepanyan
@RReverser
Apr 10 2018 18:48
at the very least you're clearly missing ref
Dylan DPC
@Dylan-DPC
Apr 10 2018 18:48
i thought i may not need that :D
Ingvar Stepanyan
@RReverser
Apr 10 2018 18:49
why? it's specifically part of example :)
You don't necessarily need braces on the right side btw
Dylan DPC
@Dylan-DPC
Apr 10 2018 18:50
ah okay.. thanks
Denis Lisov
@tanriol
Apr 10 2018 20:45
@Dylan-DPC If you want to avoid globals, you may want to use a closure instead of just a function.
Russell Cohen
@rcoh
Apr 10 2018 23:11
I want to convert an owned T to a 'static &T -- it's going to live for the entire life of the program. I tried giving the struct that owned it a lifetime parameter, but it leaked into basically every structure in the entire program
which turned into too much of hassle, since I want it to have a static lifetime anyway
Denis Lisov
@tanriol
Apr 10 2018 23:18
@rcoh Box::leak is coming to stable in 1.26
Russell Cohen
@rcoh
Apr 10 2018 23:43
perfect thanks!