These are chat archives for rust-lang/rust

11th
Nov 2017
Moggers
@Moggers
Nov 11 2017 03:01
If I have an async callback, where I want something in scope to be borrowed mutably
how do I go about doing it? Sounds like I need some kind of mutex
oh futures has its own mutexes
Judson Lester
@nyarly
Nov 11 2017 04:34
This seems like a straightforward issue and I'm being blind:
172 |   let req = hyper::client::Request::new(hyper::Method::Get,
    |       ---   ^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for `B`
    |       |
    |       consider giving `req` a type
But Request is a struct, and it's <B = Body> is a struct...
bklooste
@bklooste
Nov 11 2017 04:36
There's a generic type required for Body .. not sure of the syntax I'm just coming back
bklooste
@bklooste
Nov 11 2017 04:42
@nyarly why not hyper::client::Client::new rather than getting into request internals ?
Judson Lester
@nyarly
Nov 11 2017 07:43
@bklooste I've already got a Client, but I need to client.request() because the GET needs to be authenticated
Moggers
@Moggers
Nov 11 2017 08:49
I really dislike macros
whenever a library uses them for anything other than stuff like json!/doc! it ends up giving me the worst syntax errors
David Harvey-Macaulay
@alteous
Nov 11 2017 09:08
@Moggers Out of curiosity, which libraries are causing trouble?
Moggers
@Moggers
Nov 11 2017 09:54
rocket is the one thats giving me grief right now
@alteous
Sherab Giovannini
@Shaddy
Nov 11 2017 20:04

which one is better?

considering better => efficient, less memory consumption, and elegant.

    let undashed = isbn.split(|c| c == '-').collect::<String>();
    let undashed = isbn.chars().filter(|&c| c != '-').collect::<String>();

The goal obviously is to undash a string.

Mark Simulacrum
@Mark-Simulacrum
Nov 11 2017 20:30
split may be more optimizable in the long term, but I'd benchmark to be certain
technically, they may be close to equivalent
Jonas Platte
@jplatte
Nov 11 2017 20:30
@Shaddy You want to remove all dashes from a string?
Jonas Platte
@jplatte
Nov 11 2017 20:39
I'd use either isbn.retain(|c| c != '-'); (if you own isbn and don't need it afterwards), or let undashed = isbn.replace('-', ""); otherwise
Sherab Giovannini
@Shaddy
Nov 11 2017 21:19
uhm, interesting @jplatte
didn't knew about retain
I could use also replace but not sure if its more optimal
because you're telling to match a pattern, then replace with a content, despite the content in this case is empty
Jonas Platte
@jplatte
Nov 11 2017 21:21
well you can benchmark it
Sherab Giovannini
@Shaddy
Nov 11 2017 21:21
yep, but don't know a great tool to benchmark rusty snippets xDDDDDD
just time and huge loops
Jonas Platte
@jplatte
Nov 11 2017 21:21
but I'd be surprised if replace was less efficient than creating an iterator and then collecting it
Oh, it's pretty easy if you're on nightly
Sherab Giovannini
@Shaddy
Nov 11 2017 21:22
I am on it
I'll start to write the code while you point me in how to benchmarkt
Sherab Giovannini
@Shaddy
Nov 11 2017 21:42
running 3 tests
test bench_filter  ... bench:         137 ns/iter (+/- 84)
test bench_replace ... bench:         306 ns/iter (+/- 216)
test bench_split   ... bench:         200 ns/iter (+/- 137)
the diference is consistent between executions
pub fn undashing_with_split(target: &str) -> String {
    target.split(|c| c == '-').collect::<String>()
}

pub fn undashing_with_replace(target: &str) -> String {
    target.replace("-", "")
}

pub fn undashing_with_filter(target: &str) -> String {
    target.chars().filter(|&c| c != '-').collect::<String>()
}
i should also check retain
but replace is apparently slower
thanks for the bench awesome feature <3
Jonas Platte
@jplatte
Nov 11 2017 22:38
Can you send me the whole thing? I really want to run it myself ^^
But in any case, now you have an answer to which one is most efficient :)
Judson Lester
@nyarly
Nov 11 2017 23:31
Is there a way to hint at a type in a match?
I'd like to do match serde_json::from_slice but rustc can't infer the Ok type
I suspect I could ::<Result<Type, _> but is that really the way?