These are chat archives for rust-lang/rust

9th
Jul 2018
trsh
@trsh
Jul 09 2018 07:12
Hi! I want my struct value to be a string, and particular one: "x", "y" or "z". How to i do that? First in mind what comes is an Enum, but it kind of don't fits the bill. Note that later I wan to convert this struct in to Json using serde.
Denis Lisov
@tanriol
Jul 09 2018 07:12
Why do you want it to be a string?
trsh
@trsh
Jul 09 2018 07:14
@tanriol I will respond the final JSON to front-end (js), that understands only those strings. Kind of if(type=="x") doThat()
Denis Lisov
@tanriol
Jul 09 2018 07:15
You don't need to make it a string for this to work.
trsh
@trsh
Jul 09 2018 07:16
Basically what I want to achieve, is that nobody fucks up this type field, and doesn't write "a" instead of x, yz or z
Then front-end would not understand what to do with it
@tanriol so what do I need?
Denis Lisov
@tanriol
Jul 09 2018 07:20
Does this field actually affect the interpretation of some field in the same structure? several fields? the whole structure?
trsh
@trsh
Jul 09 2018 07:22
Im trying this now > https://pastebin.com/iwwzeCVr
I hope I can escape/asnitize special keyowrds type and enum
because it's giving compile errors
trsh
@trsh
Jul 09 2018 07:41
Anybody know how to use Word 'type' as struct field?
expected identifier, found keyword
Andrey Lesnikov
@ozkriff
Jul 09 2018 07:42
change it to type_ or something alike?
you can't use keyword as a normal identifier
trsh
@trsh
Jul 09 2018 07:42
@ozkriff but I will later serialize the object to json, so I need this particular name
trsh
@trsh
Jul 09 2018 07:43
Ouh
Tnx.. but this is Kind of lame thing for rust :/
I should be able to use whatever name for struct, and compiler should ignore
Andrey Lesnikov
@ozkriff
Jul 09 2018 07:45
Can't agree with you - there's no point in having this hack in a language when you can have it in serde. It's just a lesser evil
Ihnat
@terziele
Jul 09 2018 07:45
I think not. Every language have reserved names, isn't it?
trsh
@trsh
Jul 09 2018 07:47
Eh maybe
Michal 'vorner' Vaner
@vorner
Jul 09 2018 07:47
@terziele I'm pretty sure there are languages that don't have reserved names (there are languages that don't have any names). But that's more academical I guess ‒ most languages do have keywords.
@trsh https://rust-lang-nursery.github.io/edition-guide/2018/transitioning/raw-identifiers.html ‒ this is possible (or, will be, not stable yet). It's just not particularly comfortable to use.
trsh
@trsh
Jul 09 2018 07:48
C also disallows reserved keywords in Enums and Structs
Just checked
So im overreacting
trsh
@trsh
Jul 09 2018 07:53
JavaScript vs Rust: JS = get something done fast, and then do fixing bugs and performance issues for a looooong time, Rust = get something done in much longer period, go production -peace and heaven. My personal exp. for now. :D
@ozkriff i need nightly for that rename? :(
Andrey Lesnikov
@ozkriff
Jul 09 2018 07:59
hm? no, it should work with stable
trsh
@trsh
Jul 09 2018 08:00
The attributeserdeis currently unknown to the compiler
Ihnat
@terziele
Jul 09 2018 08:00
@vorner That's interesting.
trsh
@trsh
Jul 09 2018 08:01
I have serde and serde_json, and macros loaded from them
mhm serde-rs/serde#175
trsh
@trsh
Jul 09 2018 08:04
Ok so its my setup probl. tnx
trsh
@trsh
Jul 09 2018 08:09
I was missing #[derive(Serialize, Deserialize, Debug)]
Error was not very helpful, but glad it works now
Michal 'vorner' Vaner
@vorner
Jul 09 2018 08:18
Debug is not strictly necessary, but generally useful.
Heinz N. Gies
@Licenser
Jul 09 2018 08:47
Hello friends!
I got a question, I tried google but I don't think I can phrase it right. Basically I try to make a 'creator' frunction that returns different implementations of a trait depending on it's arguments say fn new(type: &str) -> impl Geometry {match type { "circle" => Circle::new(), "square" => Square::new(), _ => panic!("unknown Geometry")}} which fails with saying it should return Circle but does return Square.
Andrey Lesnikov
@ozkriff
Jul 09 2018 08:54
you can't do this with static dispatching, but you can return something like Box<dyn Geometry> (dynamic dispatching), though it has some runtime cost
alternatively you can use an enum for this
Heinz N. Gies
@Licenser
Jul 09 2018 08:54
Thanks Andrey! I'll give both things a try :)
Heinz N. Gies
@Licenser
Jul 09 2018 09:15
oh the enum thing works well, just one more question if you lot don't mind. I if I add something like impl Geometry to the enum now and have a draw function I got to pattern match on all implementations of Geometry in that and dispatch, is there a nicer way?
Dmitriy
@dpogretskiy
Jul 09 2018 09:17
@Licenser pretty much the only way, that's why enums aren't widely used
Heinz N. Gies
@Licenser
Jul 09 2018 09:17
:D hehe fair. I looked at the dyn stuff but it seems not to be in stable yet
thanks @dpogretskiy !
Dmitriy
@dpogretskiy
Jul 09 2018 09:17
but it's pretty good for readability, can't argue with that :)
Heinz N. Gies
@Licenser
Jul 09 2018 09:18
very true!
just hard to change and easy to add bugs if every implementation needs to be added in 3 places
Dmitriy
@dpogretskiy
Jul 09 2018 09:19
that's your good ol' expression problem
Heinz N. Gies
@Licenser
Jul 09 2018 09:19
^.^
Well I know how it works now, it makes sense :D I can deal with 3 places just need it well documented
Dmitriy
@dpogretskiy
Jul 09 2018 09:20
you can always opt in for separate structs, there is also neat trick i found long ago somewhere
Heinz N. Gies
@Licenser
Jul 09 2018 09:20
They are seperate structs for me, all implementing draw, I think the main problem I created was wanting to dynamically creating them
Dmitriy
@dpogretskiy
Jul 09 2018 09:20
struct A;
struct B;
struct C;
enum ABC {
 A(A),
 B(B),
 C(C),
}
Michal 'vorner' Vaner
@vorner
Jul 09 2018 09:20
@Licenser Box<dyn Geometry> is just a new less confusing syntax for Box<Geometry>. If dyn isn't stable yet, use it without it, you get the same thing.
Heinz N. Gies
@Licenser
Jul 09 2018 09:21
let me try again that errored on me but I might have done something stupid
Ah it tells me 'the trait cannot be made into an object that was the error
Heinz N. Gies
@Licenser
Jul 09 2018 09:26
using Box<impl Geometry> causes the same expected struct Circle found Square thingy. I think Enum it is for me :)
thanks!
Dmitriy
@dpogretskiy
Jul 09 2018 09:27
impl Geometry is not "dynamic"
it's just hidden type
so it has to be single type return anyway
Box<Geometry> should work tho
Andrey Lesnikov
@ozkriff
Jul 09 2018 09:38
"but it seems not to be in stable yet" - dyn-trait syntax was stabilized in rust 1.27 a few weeks ago - https://blog.rust-lang.org/2018/06/21/Rust-1.27.html#dyn-trait but yeah, just use Box<Geometry> syntax if you have an older compiler
Dmitriy
@dpogretskiy
Jul 09 2018 09:39
isn't it feature gated anymore?
Andrey Lesnikov
@ozkriff
Jul 09 2018 09:43
Heinz N. Gies
@Licenser
Jul 09 2018 10:44
@dpogretskiy Box<Geometry> doesn't work sadly I've made a demo: https://github.com/Licenser/geo/blob/90dfd78d94a0d5e8155552c8892df4da5de39796/src/main.rs#L25
Heinz N. Gies
@Licenser
Jul 09 2018 13:10
thanks! I feel stupid now :D
Andrey Lesnikov
@ozkriff
Jul 09 2018 13:10
you need to have some self argument if you want to use the trait for dynamic polymorphism + there's no point in having new method in the trait :)
Heinz N. Gies
@Licenser
Jul 09 2018 13:11
:+1:
Sylwester Rąpała
@xoac
Jul 09 2018 15:21
I have a HashMap<u32, Vec<u32>> and I would like to create iterator that return (hash_key1, first_value_from_vec),(hash_key1, second_value_from_vec) ...
I think I should use flat_map but I am not sure how
Sylwester Rąpała
@xoac
Jul 09 2018 15:44
@RomanAkberov thank you
Sylwester Rąpała
@xoac
Jul 09 2018 16:15
@RomanAkberov Truly I would like to imple IntoIterator but I don't know what should be as type IntoIter https://play.rust-lang.org/?gist=5edace89c3aca689b5767e02e16d4e76&version=stable&mode=debug&edition=2015
Matthias Berndt
@mberndt123
Jul 09 2018 19:06
I'm trying to debug my program with rust-lldb, but when I try to print a local variable called path, it says "reference to 'path' is ambiguous", because there is apparently another thing call path, namely std::sys::unix::path
Now if I wanted to know the latter it'd be easy to disambiguate, but for a local variable I don't know how to do it
does anybody else?
Matthias Berndt
@mberndt123
Jul 09 2018 21:09
I'm using emacs with racer, but it seems that code completion isn't quite working. I get completion for some methods on Vec, e. g. append, but not for others, e. g. iter
And apparently racer doesn't know about iter's return type either, because I'm not getting completion for the return value either :-(
Colt Frederickson
@coltfred
Jul 09 2018 22:25
Say I want a trait which extends another trait over &Self, how could I do that? For instance say I want trait Foo: Add<Output=Self> + Sub<Output=Self>, but I want the Add to be over &Self, ditto with Sub so the implementation requires fn add(&self, rhs:RHS): Self.