These are chat archives for rust-lang/rust

23rd
Jun 2018
Joseph Murphy
@JMurph2015
Jun 23 2018 06:34

Hi! So if I have a struct like this:

pub struct MyStruct {
    data: SomeConcreteData,
    reference_structs: Vec<ReferenceStruct>
}

where the reference structs are lifetime dependent on references to data in the data field, how do I specify their lifetime without making the whole upper level struct also lifetime annotated (which is silly because it contains all the data it needs at all times)?

Fredrik Portström
@portstrom
Jun 23 2018 06:39
@JMurph2015 Self-referencing structs are not supported.
Joseph Murphy
@JMurph2015
Jun 23 2018 06:40
Is t his really a self referencing struct though?
Like I guess in an abstract sense it contains other structs that reference it, but I wouldn't have thought that still counts
Fredrik Portström
@portstrom
Jun 23 2018 06:42
If one field in a struct contains references to another field in the same struct it's self-referencing. The field data could be modified within the lifetime of the struct, or the struct could be moved to a different memory address, and then the references are no longer valid. The upcoming feature async functions deals with this through pinning and unsafe code, but there's no general solution to self-referencing structs.
Joseph Murphy
@JMurph2015
Jun 23 2018 06:43
gotcha, sorry for being a noob and running up against this, it's something that is quite easy to do in the other language I mainly use (Julia) and so I'm just in the habit of doing that
Fredrik Portström
@portstrom
Jun 23 2018 06:44
It's something you often do in other languages, but doing so means the language either must have garbage collection or unsafe references.
Joseph Murphy
@JMurph2015
Jun 23 2018 06:45
yep, thanks for the pointer (no pun intended)
Fredrik Portström
@portstrom
Jun 23 2018 06:45
A solution that is good in many cases is to use numbers (indexes or IDs) instead of references. When you need something from the data field, you look it up by ID.
Andrey Lesnikov
@ozkriff
Jun 23 2018 06:51
Rc/Weak may also help in some situations
Dylan DPC
@Dylan-DPC
Jun 23 2018 07:09

sorry for being a noob

@JMurph2015 no need to be sorry :)

Fredrik Portström
@portstrom
Jun 23 2018 08:31
Why is the println! output of my doc tests captured when I run cargo test -- --nocapture? :confused:
Joseph Murphy
@JMurph2015
Jun 23 2018 09:12
@Dylan-DPC ahh, I'd just meant that was about the third time I've asked on Gitter and the problem been that it was self-referential
razuit
@razuit
Jun 23 2018 14:45
any idea how to configure VSCode with RLS extension so that “Go to definition works”? I have rust-src component installed
tandrysyawaludin
@tandrysyawaludin
Jun 23 2018 15:17
@tandrysyawaludin
anyone know how to access js file in html if i run rust?
i always got 404
Dmitriy
@dpogretskiy
Jun 23 2018 15:26
@razuit i have racer fallback for go to definition, surprisingly it works
    "rust.goto_def_racer_fallback": true,
    "rust-client.updateOnStartup": true,
    "rust-client.rls-name": "rls-preview",
    "rust-client.channel": "nightly",
    "rust-client.rustupPath": "rustup",
this is in my user settings json file
@tandrysyawaludin your question sounds a bit cryptic, you mean web assembly or something else?
razuit
@razuit
Jun 23 2018 15:38
@dpogretskiy tried that, but still not working. I am on Windows. Wondering if I need to set some environment variable or something?
Dmitriy
@dpogretskiy
Jun 23 2018 15:39
well, as far as i know current nightly rust is broken, maybe that's an issue
try using is on stable
so, the channel would be "stable", but i don't really know if rls-preview stays rls-preview
btw, did you try "cargo install racer"?
razuit
@razuit
Jun 23 2018 15:44
yes, racer alredy installed
Dmitriy
@dpogretskiy
Jun 23 2018 15:53
check out some rls/vscode-rust issues on github, probably you'll find something
also, rls doesn't work when it's updating
and this process takes, like 15 minutes
maybe it's all working already :)
razuit
@razuit
Jun 23 2018 15:55
silly thing, I tried “go to definition” on “println!” (from the standard “Hello world” code in the tutorial), but if I write something like std::process:exit and I ask to go to definition it works
maybe it isn’t supposed to work for println?
Dmitriy
@dpogretskiy
Jun 23 2018 15:55
oh, that's another story
println is a macro
and you don't want to see it :laughing:
razuit
@razuit
Jun 23 2018 15:56
oh, so it isn’t supposed to work for macros, right?
Dmitriy
@dpogretskiy
Jun 23 2018 15:56
probably not, since they are imported into scope in a different way
i guess there are ways to find where they exactly are, it's probably not just there yet
razuit
@razuit
Jun 23 2018 15:58
got it. thanks for teaching me that! :)
Dmitriy
@dpogretskiy
Jun 23 2018 15:59
np, good luck with learning rust, heyo!
razuit
@razuit
Jun 23 2018 16:00
thanks! coming from a MSVC C/C++ background, so next on my list is figuring out why that hello_world binary is so large :smile:
tandrysyawaludin
@tandrysyawaludin
Jun 23 2018 16:00
how to set session in rust?
Dmitriy
@dpogretskiy
Jun 23 2018 16:01
@razuit it has pieces of std library, println support is pretty heavy, no link time optimizations, and allocator is built into executable
you can trim it a lot
but c hello world is cheating, it relies on libc and bunch of other stuff, that's not compiled into exe
rather just somewhere in win32 folder, so it's not really large
just seems so :D
@tandrysyawaludin what kind of session you are interested about?
Jan Hlavatý
@hlavaatch
Jun 23 2018 17:35
you have silly questions - you either are trolling or are entirely misguided about what rust is
to give you benefit of doubt, you may want to look at some web framework like Rocket
Robert
@rw
Jun 23 2018 18:55
has what is "idiomatic" evolved since this blog post came out a year and a half ago? https://rust-leipzig.github.io/architecture/2016/12/20/idiomatic-trees-in-rust/
James McCoy
@jamessan
Jun 23 2018 22:49
@rw https://github.com/nrc/r4cppp/blob/master/graphs/README.md is a decent read on the subject that I found recently