These are chat archives for rust-lang/rust

27th
Mar 2019
Sam Johnson
@sam0x17
Mar 27 00:06
is there some macro that will print out the type of an expression -- I'm in a situation where I don't actually know what type it is
nvm seems like let () = [unknown thing here] does the trick
Mikail Bagishov
@MikailBag
Mar 27 04:42
@thojest String is sized type, so you can still use your enum without boxing
thojest
@thojest
Mar 27 10:41
@MikailBag thanks
silmanduin66
@silmanduin66
Mar 27 15:38
Hello, can someone tell me how i can accomplish something like this in rust
// all values are u8
d = math.sqrt((x1-x0)^2 + (y1-y0)^2)
Zakarum
@omni-viral
Mar 27 15:40
How it should round the result?
silmanduin66
@silmanduin66
Mar 27 15:42
the lower u8 is fine
Zakarum
@omni-viral
Mar 27 15:43
IIRC there is no instruction for calculating integer square root in modern processors
And there is not sqrt for integers in Rust
silmanduin66
@silmanduin66
Mar 27 15:43
should i convert to f32 , do the calculation and back to u8 ?
Zakarum
@omni-viral
Mar 27 15:44
You can do this though (((x1 - x0).pow(2) + (y1 - y0).pow(2)) as f32).sqrt().floor() as u8
red75prime
@red75prime
Mar 27 15:44
It seems easier to create a table of square roots.
Zakarum
@omni-viral
Mar 27 15:44
@red75prime Makes sense, It won't be large
Just 256 entiries
silmanduin66
@silmanduin66
Mar 27 15:46
actually i think i will extend to u32
Zakarum
@omni-viral
Mar 27 15:47
Then you can convert to f32 and back again
silmanduin66
@silmanduin66
Mar 27 15:47
ok thanks
Zakarum
@omni-viral
Mar 27 15:47
Or implement one of the integer square root algorithms
silmanduin66
@silmanduin66
Mar 27 15:47
but where can i find pow and floor
Zakarum
@omni-viral
Mar 27 15:47
pow is method defined for integers and floating point types
silmanduin66
@silmanduin66
Mar 27 15:48
is it in std ?
Zakarum
@omni-viral
Mar 27 15:48
Yes
silmanduin66
@silmanduin66
Mar 27 15:51
ok thanks
i have another question: how can i map let s say [0..5000] -> [0..255] , is there a custom function or should i write it myself ?
Iulian Rotaru
@mortimr
Mar 27 16:48

Hi, I'm currently trying to use pattern matching on Vec types. In my case I can have other Vec stored inside the one on which the match is running. My problem is that the first vec can easily be used as a slice for the match keyword, but the one contained inside of it is still of type Vec. Do you guys know if there is a way to get it as a slice there ?
This is my match

    match include.subrules.as_slice() {
        [
        Token::Terminal(TerminalToken {rule: ast::Rule::IncludeKW, ..}),
        Token::NonTerminal(
            NonTerminalToken {
                rule: ast::Rule::IncludePathString,
                subrules: [ // <== this here KO as it's Vec<Token> and not slice
                Token::Terminal(
                    TerminalToken {
                        rule: ast::Rule::FilesystemPath,
                        content: content,
                        ..
                    })
                ],
                ..
            }
        ),
        Token::Terminal(TerminalToken {rule: ast::Rule::END_OF_LINE, ..})
        ] => println!(content),
        _ => panic!("Invalid IncludeStatement AST Token")
    }

In this particular case, I'm trying not to run a second match (I will if there is no other solution), but I would love to have it all inside the same match as it would discard every possible wrong situation with one single match.

Riley Cat
@WreckedAvent
Mar 27 21:24
@silmanduin66 you can fold when the
codomain is different
but it may be more clear to just use iterators directly depending on the logic
Sam Johnson
@sam0x17
Mar 27 21:45
what does it mean when there is a ? at the end of a method call (after the ())?
matrixbot
@matrixbot
Mar 27 21:48
bspeice If the function you're calling returned a Result::Error, just return that error instead of continuing.
Ichoran
@Ichoran
Mar 27 22:59
@sam0x17 - It's an early return of the error case. The return value is an Ok<GoodThing, SomeError>, and ? immediately returns with Err<SomeError> if that's what the function gave back.