These are chat archives for rust-lang/rust

6th
Aug 2018
Andrey Lesnikov
@ozkriff
Aug 06 2018 04:20

How are you able to make the function cal without an instance of u8 or f32 ?

It's just a static method (it doesn't take any self argument), kinda like Vec::new().

Akos Vandra
@axos88
Aug 06 2018 06:11
Can I add a pattern guard into an if let expression?
Andrey Lesnikov
@ozkriff
Aug 06 2018 06:15
Not that I'm aware
Akos Vandra
@axos88
Aug 06 2018 08:06
@ozkriff thx
trsh
@trsh
Aug 06 2018 08:38
Is there anyway to concat static strings?
Tim McNamara 🐍🦀
@timClicks_twitter
Aug 06 2018 08:38
@trsh there is a concat! macro IIRC
trsh
@trsh
Aug 06 2018 08:39
Like I need static string x than includes static string y, but y is needed also on own
OK I will try
Tim McNamara 🐍🦀
@timClicks_twitter
Aug 06 2018 08:39
I'm pretty sure I saw a reddit thread about enabling this within a crate, but I don't know where
trsh
@trsh
Aug 06 2018 08:59
Not what I need
I nees smth like pub static that: &'static str = "xx"; pub static this: &'static str = "this" + that;
But I think it's no possible, and I have to addoptd diff strategy
Akos Vandra
@axos88
Aug 06 2018 10:57
@trsh, I think you could do, but I guess it’s a bit counterintuitive
  pub static thisthat : &’static str= “xxthis”;
  pub static this: &’static str = &thisthat[0..1];
  pub static that: &’static str = &thisthat[2..5];
Zakarum
@omni-viral
Aug 06 2018 10:59
Otherwise you would need to store strings as [u8; X], concant [u8; X] with [u8; Y] to get [u8; X + Y] and cast into &str where needed
Talden
@Talden
Aug 06 2018 11:32
Anyone know how to parse non-compliant JSON? The json I have sometimes contains trailing commas. I want to process the JSON but don't control it's production so the non-compliance is tripping up my attempts to parse into a Value with serde. I'm pretty new to rust so I'm quite probably just holding the tool wrong.
I'm using let value :Result<Value, Error> = serde_json::from_str(&buffer); to parse a String and get Err(Error("trailing comma", line: 1, column:298124)) back.
It's quite correct, there are several places where the generated JSON has trailing commas - all other content appears valid. I'd hoped to be able to adjust the strictness of the parser.
Talden
@Talden
Aug 06 2018 11:56
Damn. Looks like an open issue... serde-rs/json#70
Tim McNamara 🐍🦀
@timClicks_twitter
Aug 06 2018 11:57
Although you are new to Rust, I have an idea that might be worth trying
Send a slice to serde
Perhaps something like &buffer[0..buffer.len()-1]
That might feel like an awful hack, but might get you closer to where you want to be
Talden
@Talden
Aug 06 2018 12:05
The trailing commas are throughout the document. The producer appears to use a naive production for adding elements (though not consistently) where trailing commas are produced in a way that isn't JSON compatible (but is JSON5 compliant I see). I wonder if a JSON5 parser is floating around for rust - I'm hoping not to have to move away from rust for this piece of my puzzle.
trsh
@trsh
Aug 06 2018 12:06
@axos88 no way. My string are huge and constantly changing. I think I just need to concat them on runtime ;)
Michal 'vorner' Vaner
@vorner
Aug 06 2018 12:12
@Talden I know it might sound like a daunting task, but maybe it would not be that complicated to try to implement the extension and submit a MR. And, by the way, serde has its own IRC channel, the people there are usually very responsive, maybe they have an idea what to do with your problem.
Akos Vandra
@axos88
Aug 06 2018 12:12
@trsh I meant that you could probably come up with a macro that does that for you
Talden
@Talden
Aug 06 2018 12:25
@vorner Thanks. I'll ask around. I'm very new to rust so it'll be a bit of a hill-climb scratching my own itch here (but I'll pull the code and take a peek).
Akos Vandra
@axos88
Aug 06 2018 14:56
hey. Is there a way to create a class that would serialize snake_case to toml, but camelCase to JSON? I know about #[serde(rename = “foo”)], however i’m not sure if that can differentiate between the serializers
Zakarum
@omni-viral
Aug 06 2018 14:57
@axos88 It's hard to do cause Serialize::serialize method works for arbitrary serializer S
You can try to get type of serializer provided with std::any::TypeId::of
Or you can wrap your serializers and implement Serializer for wrapper
In which you will convert case
Like struct CamelCaseSerializer<S>(S) and then impl<S: Serializer> Serializer for CamelCase<S> { ... }
qq00
@qq00
Aug 06 2018 20:39

Instead of writing

struct Foo { ... }
impl Foo { ... }

is there a way to embed the "impl" directly in the "struct" ?

Andrey Lesnikov
@ozkriff
Aug 06 2018 20:43
nope
qq00
@qq00
Aug 06 2018 20:43
Alright, an excuse to write a macro! :-) /s
Denis Lisov
@tanriol
Aug 06 2018 20:52
@qq00 What for?
Ichoran
@Ichoran
Aug 06 2018 20:53
I guess you could avoid writing Foo twice if class Foo { ... } expanded to both the struct layout and data, like in many other languages.
But I think that's rather misleading, since that isn't really how things work with Rust.
qq00
@qq00
Aug 06 2018 20:54
Yeah, I don't mind the extra line.
Akos Vandra
@axos88
Aug 06 2018 22:43
Can I format!(include_str!(….), arguments) somehow?
qq00
@qq00
Aug 06 2018 22:51
I know about Vec::new() and Vec::with_capacity().
Question: is there a way to create a Vec that has n copies of a single element?
Roman Proskuryakov
@kpp
Aug 06 2018 23:03
Yes. smt like: vec.into_iter().flat_map(|x| repeat(x).take(n)).collect()
qq00
@qq00
Aug 06 2018 23:04
does rust compile away the closure, or does it call the closure for each element ?
Roman Proskuryakov
@kpp
Aug 06 2018 23:05
I don't know.
Why are you asking this?
qq00
@qq00
Aug 06 2018 23:05
It seems an awfully slow way to initialize a large structure if you have to call a closure for each element.
Roman Proskuryakov
@kpp
Aug 06 2018 23:06
Do you know any fast solution in other languages?
qq00
@qq00
Aug 06 2018 23:07

typedef struct { .... } Foo;
Foo vs[100000]
Foo f = ...
for(int i = 0; i < 100000; ++i) vs[i] = f;

This requires a copy per element, but it's not a function call per element.

In https://doc.rust-lang.org/rust-by-example/trait/clone.html ... how does "Copy and Clone" differ when deriving ?
Roman Proskuryakov
@kpp
Aug 06 2018 23:13
premature optimization is the root of all evil
qq00
@qq00
Aug 06 2018 23:14
The only reason I'm using Rust is my last language was too slow.
Roman Proskuryakov
@kpp
Aug 06 2018 23:14
but you may try to build my snippet on https://play.rust-lang.org/ and see its asm
Roman Proskuryakov
@kpp
Aug 06 2018 23:22
@qq00 why would you need to repeat each elem?
Do they have to be in the same order?
Ichoran
@Ichoran
Aug 06 2018 23:43
@qq00 - Closures are typically inlined when they can be statically resolved.
Don't assume the high-level iter-based version is slow unless you benchmark it and measure that it's slow.
Ichoran
@Ichoran
Aug 06 2018 23:48
Also, you can just let v = vec![f; 100000];
qq00
@qq00
Aug 06 2018 23:57
Ichoran: that's what I was looking for; thanks!