These are chat archives for rust-lang/rust

10th
Oct 2017
Michal 'vorner' Vaner
@vorner
Oct 10 2017 05:46
@simply-jos First, you configure how panic behaves in your Cargo.toml. So if you want, you can conigure it to unwind (which is the default). Second, well behaved libraries don't panic! unless there's a bug in the code (either in the lib or your code that calls it). Consider it something like assert in C or C++. Third, probably don't panic. If your whole application crashes and burns, whatever web server you run behind (or something else that starts you) will just restart it.
Steve Klabnik
@steveklabnik
Oct 10 2017 13:54
yeah, like, you should already be thinking about what happens if your application crashes
and have plans
this is true no matter what the language
Alexander Kureniov
@SkeLLLa
Oct 10 2017 14:36

Hello. I'm quiet new to Rust, so may be my question is quiet obvious, but I can't find any simple solution for it. It's about parsing JSON with serde_json.
So I have JSON like

{
"values": [
            [
                1507611842,
                "0.00061819"
            ],
            [
                1507611843,
                "0.000584391"
            ],
            [
                1507611844,
                "0.00062089"
            ]
        ]
}

As you can see the array has mixed-type items, which are basically "key-value" pair that encoded into array (it's a part of prometheus monitoring output).

I've tried to parse it into structs like

let values: Vec<HashMap<u64, String>> =  serde_json::from_str(my_data)?;
let values: Vec<Vec<String>> =  serde_json::from_str(my_data)?;

Googling suggest only option with writing custom deserializer for that. But maybe I'm missing something? May be there is some way to set type of array items or just to convert them on the fly?

Michal 'vorner' Vaner
@vorner
Oct 10 2017 14:46
You can get Vec<Vec<Value>> and then handle that. Value is something that can contain any valid JSON thing and you can ask its type at runtime and convert it to that type.
But someone should be tortured for doing ugly things like that. Arrays are for same-type data.
Aleksey Kladov
@matklad
Oct 10 2017 14:48
@vorner just curious, what representation serde would use for (i32, String)?
Michal 'vorner' Vaner
@vorner
Oct 10 2017 14:49
Huh, no idea. Maybe this.
Denis Lisov
@tanriol
Oct 10 2017 14:55
#[derive(Deserialize, Debug)]
struct MyData {
    values: Vec<(u64, String)>,
}
Alexander Kureniov
@SkeLLLa
Oct 10 2017 14:59
@matklad, @tanriol Thanks! That works great.
trepidacious
@trepidacious
Oct 10 2017 15:17
I was just looking at Rust again for the first time in a while - I was sure that there was no GC, but this page implies it's an official thing now?
Steve Klabnik
@steveklabnik
Oct 10 2017 15:18
that page is from july of 2014
it even has incorrect syntax on it
rust has no GC
you can use reference counting libraries if you want, just like any language. but they're not used much
trepidacious
@trepidacious
Oct 10 2017 15:23
:(
Steve Klabnik
@steveklabnik
Oct 10 2017 15:24
?
trepidacious
@trepidacious
Oct 10 2017 15:28
I was just disappointed, I've been put off Rust when I've looked at it before by the memory stuff, so I was kind of excited to think it had optional GC.
Steve Klabnik
@steveklabnik
Oct 10 2017 15:28
ah
i am used to the opposite reaction :)
it's all good though
trepidacious
@trepidacious
Oct 10 2017 15:38
I know there are performance implications of GC, and at a low level I can see that it might not really be an option, but having it somewhere in the language would be nice for higher level code that is less performance-sensitive
I don't think my use cases are really the ones Rust is intended for though
Steve Klabnik
@steveklabnik
Oct 10 2017 15:39
it depends
i would say "you'd still need to know that stuff anyway to use libraries"
and so, lots of people don't feel the need for a GC, even ones that don't need the performance neccesarily
anyway, gotta run
trepidacious
@trepidacious
Oct 10 2017 15:40
np
Michal 'vorner' Vaner
@vorner
Oct 10 2017 16:00
I think I've seen some GC as a library or a compiler plugin. But I have no idea if and how it works.
Pascal Hertleif
@killercup
Oct 10 2017 16:20
@vorner yeah manish was working on one
Michal 'vorner' Vaner
@vorner
Oct 10 2017 16:28
I'm not sure it's that one. There was one GC for like ordinary structures in rust, and another if you needed to implement a GC for let's say a javascript interpreter inside Rust. I think. It's just a vague impression, though, I may be completely wrong.
Zakarum
@omni-viral
Oct 10 2017 18:14
Are we expecting linear types soon?
Judson Lester
@nyarly
Oct 10 2017 18:36
Is the advice on error handling from the first edition of the Rust book still accurate? IOW build a crate-local type that's a union over types you need to return?
If so, is there macro for producing that type? error_union!(io::Error, regex::Error) or something?
James McCoy
@jamessan
Oct 10 2017 18:52
@nyarly Something like error_chain?
Judson Lester
@nyarly
Oct 10 2017 18:58
@jamessan That looks a lot like what I was hoping for.
Is there a generally good feeling about error_chain? (iow, I'm sure someone thinks it's terrible and I'm curious to hear the objections to it)
Denis Lisov
@tanriol
Oct 10 2017 19:39
This is a mostly accepted solution (as in "was evaluated in the official libs blitz"). There are likely significant changes before 1.0 (like migration to a proc-macro), however.
Judson Lester
@nyarly
Oct 10 2017 19:40
What's a proc-macro?
Denis Lisov
@tanriol
Oct 10 2017 19:51
To derive-based syntax like in derive-error-chain