These are chat archives for rust-lang/rust

6th
Jan 2018
Noah Weninger
@nwoeanhinnogaehr
Jan 06 2018 02:09
@Shaddy This is essentially the problem of finding a substring - if you're only working with u8s then you could use str::contains. However, if your slices are large, or the containing slice is checked against multiple smaller slices, you may benefit from implementing an algorithm like a suffix tree or KMP, which will reduce the time complexity in some cases
Noah Weninger
@nwoeanhinnogaehr
Jan 06 2018 02:17
Although, i'm not sure if it's possible to make arbitrary u8s into a str. Can't use from_utf8 because it won't accept all byte sequences, and you can't use from_utf8_unchecked because it's undefined behavior to call contains on that...
Sherab Giovannini
@Shaddy
Jan 06 2018 02:51
I see, I'll look for this or an implementation of a more optimal algo
blankhart
@blankhart
Jan 06 2018 04:58
is there a way to turn off unused import warnings on a package-wide basis (e.g. if my project has multiple binaries)
i am doing project euler problems in rust and wanted them all in the same project
i had structured them as tests, but running tests, cargo reports every error message twice and i can't figure out how to get it to stop doing that
separate binaries seems to be the easiest way but i don't wnat to have to repeat #![allow(dead_code, unused_imports, unused_variables, unused_must_use)] every time i do a new problem to keep the noise out
Sherab Giovannini
@Shaddy
Jan 06 2018 09:11
@nwoeanhinnogaehr there is unsafe fn from_raw_parts_mut<'a>(p: *mut u8, len: usize) -> &'a mut str { which I guess it will create an object from the dimensions, and if it is internally an u8 it should be working as expected right?
unchecked version works as expected also
ill take a look on contains impl
if you do a <string>.contains(another) it will create a Pattern from another
    #[inline]
    fn is_contained_in(self, haystack: &'a str) -> bool {
        self.into_searcher(haystack).next_match().is_some()
    }
its nice to see these implementations but seems too complex and with a lot of side effects
Sherab Giovannini
@Shaddy
Jan 06 2018 09:17
the algorithm is probably way better than a simple impl tho
Sherab Giovannini
@Shaddy
Jan 06 2018 09:25
just for conclude, it is possible to use it as a pattern searcher, but it will fail at printing as string
don't if its possible to bench from playground but this is the code that verifies that all char range(0-255) is well detected as a subset. https://play.rust-lang.org/?gist=a9558fe73e35a5b7782971c8c90d8f10&version=undefined
running 2 tests
test bench_str_comparison ... bench:      18,239 ns/iter (+/- 485)
test bench_u8_comparison  ... bench:      23,537 ns/iter (+/- 592)
David Harvey-Macaulay
@alteous
Jan 06 2018 09:37
It's a long shot but is there any emacs package for reading rustdoc?
That would be pretty useful.
NOP0
@NOP0
Jan 06 2018 11:46
Looking through the tracking issues, I dont seem to figure out if impl trait in argument position is in nightly yet. Would have tried but not at computer atm. anyone know?
blankhart
@blankhart
Jan 06 2018 14:48
it works for me behind the feature gate universal_impl_trait
NOP0
@NOP0
Jan 06 2018 14:50
@blankhart thanks! thats great
blankhart
@blankhart
Jan 06 2018 15:05
@NOP0 thanks for letting me know about it. it explains why the feature gate for the return position was named conservative_impl_trait. the usefulness is still limited somewhat by the inability to type alias impl Traits, though there seem to be some workarounds
blankhart
@blankhart
Jan 06 2018 16:50
oh it looks like there is an experimental trait_alias gate but not yet implemented on nightly #41517
something to look forward to
Michal 'vorner' Vaner
@vorner
Jan 06 2018 17:30
Today I've met something acting really non-obviously in rust. I already found the problem, but if anyone wants to try for themselves, here it is: https://play.rust-lang.org/?gist=aacf856cb0a3da75cea23bc73d9b8dcc&version=stable
Now, the question is, is there something that can be done with this trap so nobody else falls into it?
Aleksey Kladov
@matklad
Jan 06 2018 17:58
@vorner interesting! I think the solution to general problem is not to add methods to smart pointers. In this particular case, nothing can be done, because BorrowMut is a trait, but I don't think that one actually uses BorrowMut a lot in real life.
Michal 'vorner' Vaner
@vorner
Jan 06 2018 18:02
I know. However, a newbie friend asked me to help find out why his code doesn't compile, while he copied it from an example somewhere and it does compile on its own. I have no idea why he had that BorrowMut in scope, he obviously experimented without real understanding, but I guess this makes it hard for unexperienced people.
I guess the error message explains exactly what happened, in retrospect, but it doesn't help much.