How can I initialize fields of a struct in static context? [duplicate] - static

This question already has answers here:
How can you make a safe static singleton in Rust?
(3 answers)
How do I create a global, mutable singleton?
(7 answers)
Closed 5 years ago.
I need a "default", de facto an empty struct that I will be returning as a default value. Since it is read only, I don't want to pollute the memory by creating it N times, but rather return it only once
#[derive(Debug,PartialEq)]
pub struct Vocabulary {
literal_names: Vec<String>,
symbolic_names: Vec<String>,
display_names: Vec<String>,
max_toke_type: usize,
}
static EMPTY_VOCABULARY:Vocabulary = Vocabulary{
literal_names: Vec::new(),
symbolic_names: Vec::new(),
display_names: Vec::new(),
max_toke_type: 0 ,
};
This fails, considering function calls are not allowed in static context. How can I initialized these fields then?

Related

How to call a mutating function on a struct while looping throught an iterable member of the same struct? [duplicate]

This question already has answers here:
How can I call a mutating method while holding a reference to self?
(1 answer)
Why does refactoring by extracting a method trigger a borrow checker error?
(2 answers)
Closed 11 months ago.
Imagine this scenario: you have an object containing an iterable. You want to call a function implemented on the same object, which doing mutation on another member of the "self", lets say to use an sqlite connection or draw content to an sdl canvas, based on the content of our array.
You cant do that on a trivial way because when you setting up your foreach loop you borrowing the self, and cant borrow it again with mutability.
Is there any solution for this?
How rust people dealing with objects if the whole struct either referenced mutable or immutable and we cant deal with individual members separately?
struct Stuff (i32, Vec<(i32, char)>);
impl Stuff {
fn fun1(&mut self) {
for item in &self.1 { //borrowing immutable
self.0 += item.0; // this works fine
self.do_mutation(); // but here im trying to borrow it mutable and
// it is making the cute crab angry and sad.
dbg!((item.1, self.0));
}
}
fn do_mutation(&mut self) {
//another function doing some crazy stuff...
}
}
fn main() {
let mut object = Stuff(0, vec![(1,'a'),(2,'b'),(3,'c')]);
object.fun1();
}

Inter-nested struct type in C [duplicate]

This question already has answers here:
Structs that refer to each other
(4 answers)
Closed 2 years ago.
I want to create 2 structs that are nested inside each other, but C won't allow this as when I define the first struct, the second struct is still not defined. How to solve this?
code
what you need to do is add pointers to refer to the other structs :
struct y;
struct x {
struct y * yy;
};
struct y {
struct x * xx;
};

C structs: Can a function argument be used to deference a struct pointer [duplicate]

This question already has answers here:
How can I access structure fields by name at run time?
(5 answers)
Passing a struct member NAME to function in C?
(1 answer)
How to access members of a `struct' according to a value of a string?
(3 answers)
Closed 5 years ago.
I'm learning about OOP C and I want to write the equivalent of getters and setters.
I have the following struct.
typedef struct {
int item0;
int item1;
int item2;
etc...;
} ItemsStr;
typedef ItemsStr * ItemsPtr;
items_get_item(ItemsPtr item, itemname) {
return item->itemname;
}
I realize that the syntax is invalid but is there a way to accomplish the above?
I also realize that I could write a switch statement but I was wondering if there was a way to use the parameter passed in to deference.
You can use offsetof():
int items_get_item(ItemsPtr items, size_t offset) {
return *(int*)((char*)items + offset);
}
Then the caller does this:
items_get_item(items, offsetof(ItemsStr, item1))

When to use pointers in C [duplicate]

This question already has answers here:
C best practices, stack vs heap allocation
(4 answers)
Closed 6 years ago.
I have a struct like this:
typedef struct {
int hi;
} my_struct;
Is there an advantage in using this:
my_struct *test = malloc(sizeof(my_struct));
test->hi = 1;
Instead of this:
my_struct test;
test.hi = 1;
No, usually it's quite the opposite. If you can use the format to satisfy your requrement
my_struct test;
test.hi = 1;
then it's always better, less overhead in runtime. There's no advantage in general of using memory allocator functions, when you can do without them.

what are these different in array initializing in c? [duplicate]

This question already has answers here:
How to initialize all members of an array to the same value?
(26 answers)
Closed 8 years ago.
My struct is as follow.
typedef struct user {
char *name;
int age;
} User;
Now when I am creating an array, I wonder what is the different between below both statments.
User users[10];
vs
User users[10] = {};
The first (User users[10];) do not erase the flash or memory so it is possibel that you have things on this memory from the Programm befor

Resources