These are chat archives for rust-lang/rust

5th
Oct 2017
mnivoliez
@mnivoliez
Oct 05 2017 07:43
Hello, I got some trouble with lifetime. Here's the code: https://gitlab.deep-nope.me/mnivoliez/raytracer/blob/master/src/intersect.rs
The problems start at line 27. I think I get why it's not happy but I don't know what can I do about it
Aleksey Kladov
@matklad
Oct 05 2017 07:44
I think you need &'a &Vec<Object>
Sergei Pepyakin
@pepyakin
Oct 05 2017 07:45
that was fast!
mnivoliez
@mnivoliez
Oct 05 2017 07:45
I'll try that
seems to work.
Thank you :)
Aleksey Kladov
@matklad
Oct 05 2017 07:57
@mnivoliez it would be more rustic to use &'a [Object] instead of &'a Vec<Object>, because you don't use any vec specific methods.
mnivoliez
@mnivoliez
Oct 05 2017 07:59
then should I pass it like a slice?
Aleksey Kladov
@matklad
Oct 05 2017 08:00
Vec has a deref coercion to [], so the call sites wouldn't have to change at all
mnivoliez
@mnivoliez
Oct 05 2017 08:08
Oh
That's awesome
Thomas
@thowenzel
Oct 05 2017 10:55
Hi, I'm very new to rust. So my question is maybe a little bit simple...
How can I sort strings ignoring case? I read text from a file into a string and split the content. The I can sort this, but how can I ignore the upper/lower case?
...
let mut strings: Vec<&str> = data.split_whitespace().collect();
strings.sort();
Dmitriy
@dpogretskiy
Oct 05 2017 10:58
you can transform it to lower case and then sort, it's not really effective but simple
i am having some lifetime issues with serde :(
if anyone could help that would be awesome
Thomas
@thowenzel
Oct 05 2017 11:00
Yea, but I need the original strings, because I will write them sorted in a new file
Dmitriy
@dpogretskiy
Oct 05 2017 11:00
you just store them side by side with lower case ones
Grégoire Geis
@71
Oct 05 2017 11:01
@thowenzel Looks like you can sort by key or using your own comparison as well.
Thomas
@thowenzel
Oct 05 2017 11:02
Thanks
Denis Lisov
@tanriol
Oct 05 2017 11:14
@lemonxah Simple test: who owns the slice that Response::Errors contains a reference to?
Laurențiu Nicola
@lnicola
Oct 05 2017 12:01
Tho
Thomas : what language is your text in?
@thowenzel *
lemonxah
@lemonxah
Oct 05 2017 12:15
@tanriol i guess the Response will own it
Thomas
@thowenzel
Oct 05 2017 12:26
@lnicola Your mean the code? It's rust. Or what do you mean?
Andrey Lesnikov
@ozkriff
Oct 05 2017 12:33
I think it was about different human languages treating a lower/upper case idea differently
Laurențiu Nicola
@lnicola
Oct 05 2017 12:37
Yeah, like German until recently
And I think each language has its own sorting rules
Denis Lisov
@tanriol
Oct 05 2017 12:44
@lemonxah Then you need to use a Box and Vec instead of references
Laurențiu Nicola
@lnicola
Oct 05 2017 12:45
rust-lang-nursery/thanks@d9a0576 I found this which doesn't seem ideal, but might still be better than doing nothing
Thomas
@thowenzel
Oct 05 2017 12:51
Ah, now I understad. Yes I need the lower/upper case for files with german words.
Laurențiu Nicola
@lnicola
Oct 05 2017 12:58
Maybe that approach will work, other you might need to look for ICU bindings
The one in my link
You might also want ß to compare as equal to ss if that's how it works
At least you're not sorting Chinese
any help
Sergey Noskov
@Albibek
Oct 05 2017 13:23
@lemonxah I suppose you need Add, not Sum
Fra ns
@snarf95_twitter
Oct 05 2017 13:23
How do I get backtraces on exceptions running on windows?
I've tried SET RUST_BACKTRACE=1
and then Cargo run
Andrey Lesnikov
@ozkriff
Oct 05 2017 13:25
you can set the flag from the app itself: std::env::set_var("RUST_BACKTRACE", "1");
Fra ns
@snarf95_twitter
Oct 05 2017 13:26
thanks @ozkriff
Andrey Lesnikov
@ozkriff
Oct 05 2017 13:29
though set RUST_BACKTRACE=1 should work too :-\
lemonxah
@lemonxah
Oct 05 2017 13:30
@Albibek no i want sum becuase i am not adding two NumbesI to gether i am summing the numbers in the NumberI together
Andrey Lesnikov
@ozkriff
Oct 05 2017 13:32
@lemonxah
   |
69 |     let () = v.sum();
   |         ^^ expected associated type, found ()
   |
   = note: expected type `<A as Sum>::output`
              found type `()`
Jonas Platte
@jplatte
Oct 05 2017 13:33
@lemonxah The problem is that there is no guarantee that all types that implement A (for which your function doit is defined) have an output type that can be converted to f64.
Andrey Lesnikov
@ozkriff
Oct 05 2017 13:33
^ v.sum returns <A as Sum>::output so you should provide some conversion method yourself
lemonxah
@lemonxah
Oct 05 2017 13:33
@jplatte yeah i know that what i am trying to get to is that how can i force that all output types will have Add ?
Jonas Platte
@jplatte
Oct 05 2017 13:33
In fact, there is no way to specify that a type should be convertible via as.
lemonxah
@lemonxah
Oct 05 2017 13:34
yeah the convertable i dont worry about that much but how can i ensure that it is primative?
is there some constraint i can put on it somehow?
Jonas Platte
@jplatte
Oct 05 2017 13:35
You can't, unfortunately
You could have an Add bound
lemonxah
@lemonxah
Oct 05 2017 13:35
i was thinking the same thing but wasn't sure how to do that
Jonas Platte
@jplatte
Oct 05 2017 13:36
let me try to rewrite your example..
lemonxah
@lemonxah
Oct 05 2017 13:37
but i guess my biggest issue that i have right now is that when impl Sum for NumbersI .. i do specify that the output type is isize, why then doesn't the compiler know that it is isize
Jonas Platte
@jplatte
Oct 05 2017 13:38
The compiler does know that. But your function doit isn't defined just for NumbersI, it's defined for anything that implements your Sum and Div traits.
lemonxah
@lemonxah
Oct 05 2017 13:38
yeah .. that was obvious and after i typed it i realised that
just having some fist fights with the type system :)
Jonas Platte
@jplatte
Oct 05 2017 13:40
This is how your function could work...
fn doit<A>(v: &A) -> f64
where
    A: Sum + Div,
    <A as Sum>::output: std::ops::Add<f64, Output = f64>,
{
    match v.div() {
        Ok(f) => v.sum() + f,
        Err(_) => 0f64
    }
}
But then you run into the problem that isize doesn't implement Add<f64>.
lemonxah
@lemonxah
Oct 05 2017 13:40
well what if v.sum returns i64 instead of f64?
yeah
Jonas Platte
@jplatte
Oct 05 2017 13:41
same
Wait
i64 instead of f64?
sum has a generic return type, not f64
Also apparently there is no impl Into<f64> for isize (or impl From<isize> for f64) either, so you need yet another solution ^^
lemonxah
@lemonxah
Oct 05 2017 13:42
no i mean v.sum is generic so you wouldn't know what it is but it will be something that is going to be convertable to f64 as an example
is there for i64? or i32?
where did you find that ?
Jonas Platte
@jplatte
Oct 05 2017 13:44
compiler error message
error[E0277]: the trait bound `f64: std::convert::From<i64>` is not satisfied
  --> src/main.rs:85:20
   |
85 |     println!("{}", doit(&n));
   |                    ^^^^ the trait `std::convert::From<i64>` is not implemented for `f64`
   |
   = help: the following implementations were found:
             <f64 as std::convert::From<i8>>
             <f64 as std::convert::From<i16>>
             <f64 as std::convert::From<f32>>
             <f64 as std::convert::From<u16>>
           and 3 others
   = note: required because of the requirements on the impl of `std::convert::Into<f64>` for `i64`
   = note: required by `doit`
(this was with i64 now)
The reason for the other impls missing is that From / Into only allow lossless conversions
lemonxah
@lemonxah
Oct 05 2017 13:47
than you so much
i didn't even know about .into
i have so much to learn about this typesystem and langauge still
Hans W. Uhlig
@huhlig
Oct 05 2017 14:47
is it possible to selectively call macro expansions?
to see how a specific macro will expand
Hans W. Uhlig
@huhlig
Oct 05 2017 14:52
That expands everything
Andrey Lesnikov
@ozkriff
Oct 05 2017 14:56
I haven't heard about anything better yet :(
Laurențiu Nicola
@lnicola
Oct 05 2017 14:56
There was a trace_macro! or something like that
Hans W. Uhlig
@huhlig
Oct 05 2017 14:56
darn
Laurențiu Nicola
@lnicola
Oct 05 2017 14:56
On phone now, can't Google
I mean I can, but..
Hans W. Uhlig
@huhlig
Oct 05 2017 14:57
It's fine
It's something we need for good macro support in IDEs
being able to specific a specific macro invocation and see its natural expansion without the mass include of every derrive import
Laurențiu Nicola
@lnicola
Oct 05 2017 14:58
Trace_macros and log_syntax
DrRacket has an interactive macro stepper
Hans W. Uhlig
@huhlig
Oct 05 2017 14:59
DrRacket? Person or program
Laurențiu Nicola
@lnicola
Oct 05 2017 14:59
Racket (Scheme) IDE
Rust macros are somewhat inspired from the ones in Scheme
So it would make sense to steal some tooling ideas
Hans W. Uhlig
@huhlig
Oct 05 2017 15:00
yeah
Andrey Lesnikov
@ozkriff
Oct 05 2017 15:00
Hans W. Uhlig
@huhlig
Oct 05 2017 15:00
Why is all the debugging stuff only in nightly
Laurențiu Nicola
@lnicola
Oct 05 2017 15:02
Because it's somewhat linked to the compiler internals, I suppose
And the API might change in the future
Maybe it will get renamed to macroexpand and macroexpand-1, like lisp calles them, maybe it will look completely different l
Hans W. Uhlig
@huhlig
Oct 05 2017 15:05
ahh
Laurențiu Nicola
@lnicola
Oct 05 2017 15:06
Hard problem, so nobody found a good solution yet :-)
Kieran
@kieraneglin
Oct 05 2017 22:11
Hey! Is this a place for questions, or is this more centered on the meta of Rust?
Denis Lisov
@tanriol
Oct 05 2017 22:12
A place for questions :-)
Kieran
@kieraneglin
Oct 05 2017 22:12
I'm trying to find a way to convert an int to and from base 38. Is there an easy way to do this that I'm missing out on?
In JS, I'd use <num>.toString(38) and parseInt(<str>, 38)
Grégoire Geis
@71
Oct 05 2017 22:19
Nothing built-in when it comes to converting to base 38, see rust-lang/rust#27728
Kieran
@kieraneglin
Oct 05 2017 22:25
Thank you! I'll see what I can come up with.
from_str_radix can handle up to base 36 (which I can deal with). Is there a way to convert to base 36 from base 10?
I'm confused if you mean there's nothing built-in for converting from base 10 full-stop, or if there's nothing to convert to base 38 specifically
Grégoire Geis
@71
Oct 05 2017 22:31
I'm not aware of the limits of from_str_radix; built-in conversions are for: base2, base10, and base16
Denis Lisov
@tanriol
Oct 05 2017 22:43
@kieraneglin Up to base 36 the digits are mostly obvious (0-9, then a-z). However, the set of digits for base 38 is not obvious.