These are chat archives for rust-lang/rust

12th
Dec 2018
gitleet
@gitleet
Dec 12 2018 02:10
Do any of the rust IDE plugins provide hover support to tell you the return type?
Fuzen
@Fuzen-py_gitlab
Dec 12 2018 02:24
Any of the plugins that have RLS support @gitleet
David O'Connor
@David-OConnor
Dec 12 2018 03:29

Hey dudes: Q on lifetimes and closures: Is there an easy fix to this, do I need to provide more details, or do I need to alter my approach? With this closure:

|ev| MyEnum::A(id, ev))

I receive the following error:

error[E0373]: closure may outlive the current function, but it borrows `id`, which is owned by the current function
...
                                               |ev|              
    |                                        ^^^^                  ----- `id` is borrowed here
    |                                        |
    |                                        may outlive borrowed value `id`

If I pass only the the closure input to A, or only id (Which originates outside the closure), it's fine. I've tried a few variants of Lifetime syntax from the Rust Book, with no resolution

David Vo
@auscompgeek
Dec 12 2018 03:32
you might want to consider moving those values into the closure rather than borrowing them
David O'Connor
@David-OConnor
Dec 12 2018 03:34
What specifically do you mean? Of note, both of the values passed are integers (usize and u32), but I've tried all sorts of deref/borrow/casting patterns with no success
David Vo
@auscompgeek
Dec 12 2018 04:12
move |ev|. Seems odd that it's not being copied though.
or maybe it isn't that odd. I'm not entirely sure how Copy types are supposed to interact with closures.
David O'Connor
@David-OConnor
Dec 12 2018 06:19
Oh my! This solution evidently fits in the first category I listed. Brilliant; thank you!
Michal 'vorner' Vaner
@vorner
Dec 12 2018 07:55
Hello. Is there a way a generated code or macro can change a little bit depending on if it is included in 2015 or 2018 edition crate?
aohan237
@aohan237
Dec 12 2018 08:30
``````
#[derive(Debug)]
enum List {
    Node(i32,Box<List>),
    Child(Vec<Box<List>>),
    Nil,
}
use List::{Child,Node, Nil};
Compiling playground v0.0.1 (/playground)
error[E0658]: imports can only refer to extern crate names passed with --extern on stable channel (see issue #53130)
--> src/main.rs:7:5
why?
here is the playgroud
why cant i use enum fields
Michal 'vorner' Vaner
@vorner
Dec 12 2018 08:32
Playground defaults to rust2018 and there were some changes around how use works. In particular here, List probably refers to a crate called List. What you most likely need is something like self::List::{...}
aohan237
@aohan237
Dec 12 2018 08:33
yeah, thanks
David Vo
@auscompgeek
Dec 12 2018 08:40
@vorner seems like an odd question. why do you want to be able to detect that?
Michal 'vorner' Vaner
@vorner
Dec 12 2018 08:54
@auscompgeek For example here it would make some sense: danburkert/prost#140. The problem is, there's a build.rs that generates a file that then gets included. And because imports work differently, the currently generated code doesn't compile nicely if included into 2018 crate.
Similar if you have a macro that eg creates snippet like this:
#[derive(StructOpt)]
struct Stuff {
Under 2015, you'd have #[macro_use] extern crate structopt;. Under 2018, you'd prefer to have it generate #[derive(structopt::StructOpt)] ....
But maybe there's a better way how to handle that?
David Vo
@auscompgeek
Dec 12 2018 09:08
you can use structopt::StructOpt; can't you?
Michal 'vorner' Vaner
@vorner
Dec 12 2018 09:11
Yes, you can. But if you're a macro, you want the user not to care. And doing a use for the user is probably wrong.
Tim Robinson
@1tgr
Dec 12 2018 15:14
You would think there was a #[cfg(...)] option that checks the edition
But I don't see anything on Google
Tim Robinson
@1tgr
Dec 12 2018 15:25
I asked on IRC #rust
Artemov Ivan
@ZOXEXIVO
Dec 12 2018 21:38
Hello! I'm newbie to Rust and i only can't deserialize simple json, which i read from server. It may be related to encoding, because, if i assing string, containing this json - it work well
 let url = format!("https://someurl/api/mention/fresh/{}", min_timestemp); //server return ["string1","string2","string3"]

    let mut json_buffer = String::new();

    let result = reqwest::get(string_to_static_str(url))
    .unwrap()
    .read_to_string(&mut json_buffer);

    let res: Vec<String> = serde_json::from_str(&json_buffer).unwrap(); // !!!!! thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("expected value", line: 1, column: 1)', libcore\result.rs:1009:5
Tim Robinson
@1tgr
Dec 12 2018 21:40
You should do .read_to_string(&mut json_buffer).unwrap();
If it was an encoding problem then that line would fail, not the serde_json::from_str call
Artemov Ivan
@ZOXEXIVO
Dec 12 2018 21:47
@1tgr no, it called without errors with unwrap
Tim Robinson
@1tgr
Dec 12 2018 21:48
Try putting in a println!("{}", json_buffer); before the call to from_str
Artemov Ivan
@ZOXEXIVO
Dec 12 2018 21:57
 Finished dev [unoptimized + debuginfo] target(s) in 2.20s
     Running `target\debug\static_g.exe`
["https://someurl/1478","https://someurl/player"]
Tim Robinson
@1tgr
Dec 12 2018 22:01
serde_json::from_str can deserialize that JSON so I'm guessing the problem is somewhere else
Denis Lisov
@tanriol
Dec 12 2018 22:02
Try a println!("{:?}", json_buffer);
My wild guess is that the server reply contains the byte order mark (BOM) which serde_json does not handle.
Artemov Ivan
@ZOXEXIVO
Dec 12 2018 22:07
Yes, first byte is "\u{feff}
Denis Lisov
@tanriol
Dec 12 2018 22:07
That's exactly the BOM.
Artemov Ivan
@ZOXEXIVO
Dec 12 2018 22:07
thanks
Denis Lisov
@tanriol
Dec 12 2018 22:08
Technically this is incorrect as it is illegal in JSON, so it seems to be the server's fault.
gitleet
@gitleet
Dec 12 2018 22:18
When using the rust Dockerfile, how to I get the nightly tag?
I tried this: FROM rust:nightly
ah, got it: FROM rustlang/rust:nightly :)