These are chat archives for rust-lang/rust

13th
Apr 2018
Sai Zeng
@zengsai
Apr 13 2018 08:34

hi, I'd like to write a macro which return a type according given string var, like:

macro_rules! get_type {
        ($struct_name:expr) => (::somemod::$struct_name)
}

let name = "File";
let file = get_type!(name).open(...);

but this not work, compiler says: ident expected, found expr.

is there any ideas to fix this?

csmoe
@csmoe
Apr 13 2018 11:19
@zengsai you might misundertand the definition of expr and ident, check out the refercence: https://doc.rust-lang.org/stable/reference/
Zakarum
@omni-viral
Apr 13 2018 12:52
How to automatically make cargo test to enable feature?
Michal 'vorner' Vaner
@vorner
Apr 13 2018 12:53
What do you mean, automatically? You can do cargo test --fearutes 'fearute1 feature2'. Isn't that enough?
Or you can have default features.
Zakarum
@omni-viral
Apr 13 2018 12:53
I want all features to be enabled by default for cargo test
But not for other cargo commands
Hm. I can specify optional dependencies in dev-dependencies to make them non-optional for testing
Paul Masurel
@fulmicoton
Apr 13 2018 13:59

![cfg_attr(test, feature(feature1))]

![cfg_attr(test, feature(feature2))]

...

is too fastidious?

Sai Zeng
@zengsai
Apr 13 2018 14:01
@csmoe I do known what expr and ident mean. what I'm saying here is that I'd like to create a macro, which can return a type according to given string var. any ideas?
Paul Masurel
@fulmicoton
Apr 13 2018 14:05
according to a string like: let name = "File"; no it is not possible
This is differnet than what you want to do but the following is possible : https://gist.github.com/rust-play/a5caf7b9ebc7d93d987048692ebf8183
Andy Grove
@andygrove73_twitter
Apr 13 2018 14:50
Hi. I'm struggling with storing a struct in a HashMap ...
let acc = m.entry(geo_code).or_insert_with(create_accumulator());
where
fn create_accumulator() -> Rc<RefCell<Accumulator>>
compiler complains with the traitstd::ops::FnOnce<()>is not implemented forstd::rc::Rc<std::cell::RefCell<Accumulator>>`
my goal is to store a mutable struct in hashmap so I can perform some aggregation on a large data set
Andrey Lesnikov
@ozkriff
Apr 13 2018 14:52
try something like let acc = m.entry(geo_code).or_insert_with(create_accumulator); or let acc = m.entry(geo_code).or_insert_with(|| create_accumulator());
Andy Grove
@andygrove73_twitter
Apr 13 2018 14:52
oh... right, I need to pass the function not invoke the function
yeah that fixed it ... thanks @ozkriff
Andrey Lesnikov
@ozkriff
Apr 13 2018 14:52
:thumbsup:
Andy Grove
@andygrove73_twitter
Apr 13 2018 14:53
I shouldn't attempt Rust before my second cup of coffee
Dylan DPC
@Dylan-DPC
Apr 13 2018 15:23
haha