Can you add a new field to a struct after initialization? [duplicate] - struct

This question already has answers here:
Is it possible to define structs at runtime or otherwise achieve a similar effect?
(1 answer)
Is it possible for one struct to extend an existing struct, keeping all the fields?
(5 answers)
Closed 2 years ago.
struct Foo {
bar: i32,
}
let foo_var = Foo { bar: 16 };
// I want to do something like this
foo_var.new_field: u8 = "new field".to_string();
// which lets me then call
println!("{}", foo_var.new_field);
// which would print "new field"
I know I could:
implement it as a function, but I want to have a consistent "style", so I want to avoid parenthesis for foo_var.new_field().
implement it as a default, but it would be cool if I could implement this in another script (like you can do with impl for functions) without always adding a field to the struct and the default.

Related

How is it possible to take an array with any size as a generic type in rust? [duplicate]

This question already has answers here:
Is it possible to control the size of an array using the type parameter of a generic?
(2 answers)
Closed 27 days ago.
I would like to have a generic struct, that has an array of MyType in it. It can be any size but can not be changed, that is why I want to use generic.
I tried it like this:
pub struct MyStruct<[MyType, COUNT]> {
data: [MyType, COUNT],
}
I get syntax error expected COMMA without any useful guides from the compiler.
The correct syntax is
pub struct MyStruct<MyType, const COUNT: usize> {
data: [MyType; COUNT],
}
The parameter COUNT needs to be annotated as a constant. There are no square brackets around the type parameters. The delimiter between the type and the count in an array type is a semicolon, not a comma.

What's the point of an Impl block without struct fields in rust? [duplicate]

This question already has answers here:
When to use impl on an empty struct [closed]
(1 answer)
What is a real world example of using a unit struct?
(2 answers)
Closed last month.
I don't really understand why there is the case where one would create an empty struct like this:
pub struct Foo;
and then subsequently have this:
impl Foo {
...
}
It just doesn't make sense to me, why not just create a mod within the crate with a file that has the functions within the impl block above?

How to insert struct into vector that lives inside the HashMap? [duplicate]

This question already has answers here:
How to lookup from and insert into a HashMap efficiently?
(2 answers)
Closed 2 years ago.
I have the struct that looks like:
pub struct MonthlyProjection {
pub sequence: u32,
pub total_deposit: f64,
}
I have this function which takes reference of the vector that contains these structs:
fn generate_projections(simulation_results: &Vec<MonthlySimulationResult>)
Inside this function, I have to group the MonthlyProject structs by their sequences, and do some calculation with it.
So here is my logic:
Create mutable HashMap<u32, Vec<MonthlySimulationResult>>, called result_map
For loop over the simulation_results vector
If the result_map already has the vector for the given sequence number, then insert the item into that existing vector, then update the result_map
If the result_map does not have the existing vector for the given sequence number, then create new vector, insert the struct, and update the result_map
How would you do this?
If you don't mind pulling in the extra dependency, maybe look at itertools:
https://docs.rs/itertools/0.9.0/itertools/trait.Itertools.html#method.group_by
Otherwise, you can write a for-loop using the entry API:
let mut result_map: HashMap<u32, Vec<_>> = HashMap::new();
for projection in simulation_results {
result_map.entry(projection.sequence).or_default().push(projection);
}
result_map
(Playground.)
Note that you specified a MonthlyProjection type but then you specified the function header and its return type with MonthlySimulationResult, so not clear what's going on there. Also, you pass in a reference to the Vec<MonthlyProjection> but return a transitively owned MonthlySimulationResult, so you need to do something there.

Mapping of Rust struct types to integers [duplicate]

This question already has answers here:
How can I statically register structures at compile time?
(3 answers)
Generating unique IDs for types at compile time
(3 answers)
Does Rust track unique object ids and can we print them?
(1 answer)
Closed 3 years ago.
I'm rolling my own entity component system.
Is there a way of mapping struct types to integers?
I could implement a toInt() for each struct, but it feels inelegant to execute code to get a number which could be known at compile-time.
I'm rather not take care of the numbering myself (it doesn't need to be stable) and just "register" a handful of struct types with a "numberer" at compile time.
I'm looking for an API along the lines of:
struct Foo {
...
}
struct Bar {
...
}
struct Baz {
...
}
struct Quux {
...
}
register!(Foo, Bar, Baz);
numberOf!(Foo) == 0;
numberOf!(Bar) == 1;
numberOf!(Baz) == 2;
numberOf!(Quux) == failure of some kind;
Is there a way of doing this?

Can an array length constant be defined for a library in another project? [duplicate]

This question already has answers here:
Is it possible to control the size of an array using the type parameter of a generic?
(2 answers)
What expressions are allowed as the array length N in [_; N]?
(1 answer)
Closed 4 years ago.
I have a library that has a statically-sized array in a struct:
const SIZE: usize = 32;
pub struct MyStruct {
ints: [u32; SIZE]
}
Can I define the constant SIZE somewhere else? More specifically, can I somehow define it in the executable project that uses this library so that it can be tuned to the needs of that project?
At time of writing this is not possible. It requires a const generic argument:
pub struct MyStruct<const SIZE: usize> {
ints: [u32; SIZE]
}
But this is not implemented yet. We expect to land this feature in late 2018.

Resources