Global constant initialized at runtime in Rust? [duplicate] - rust

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.
Is is possible to define a global constant, value of which is computed at the beginning of the runtime? Something like
static START_TIME: time::Timespec = time::get_time();
if it were possible. static and const declaration requires compile time value (calls in constants are limited to struct and enum constructors) and let can't be put outside function (error: expected item, found `let`).

I think something like lazy_static can help with this.

Related

Is there a single bit type if not how to reprent gates logic that work only with bits as input and outpu [duplicate]

This question already has answers here:
How do you set, clear and toggle a single bit in Rust?
(3 answers)
What is similar in Rust to the colon operator in C? [duplicate]
(1 answer)
How does a C-style struct with a bitfield get represented in a Rust #[repr(C)] struct?
(1 answer)
Closed 1 year ago.
Is there a single bit type in Rust?
Something equivalent to C: unsigned x:1;
The "1" represent the size, in this case one bit.
Edit
What should I do then if I want to do/represent the gates logic:
"And" gate two input(bit input) and one output bit
Same for the "Or" gate and so on...
Should I just ignore the size of the memory and work with u8?
Thanks

Rust backpointers [duplicate]

This question already has answers here:
Why can't I store a value and a reference to that value in the same struct?
(4 answers)
Shared circular references in Rust
(1 answer)
Closed 3 years ago.
I am learning Rust from a C++/Java background, and I have the following pattern
struct Node<'a> {
network_manager: NetworkManager<'a>,
}
struct NetworkManager<'a> {
base_node: &'a Node<'a>,
}
The node contains the threadpool that the NetworkManager uses to "handoff" messages once they've been processed. Because of the recursive call, it is not possible to set the base_node field in the NetworkManager immediately. In Java, I would leave it as null and have a second method that is called after the constructor called initialise(BaseNode node) that would set the base_node field (ensuring that there are no calls to the network manager before initialise is called).
What is the idiomatic way of doing this in Rust? The only way I can think of is to make base_node an Option type, but this seems suboptimal.
In general, what is the "right" way in Rust to deal with situations where A points to B and B points to A, and where (as in my case), refactoring is not possible?
From my experience, these situations are very different from other languages. In "safe, simple, everyday Rust" having backpointers/pointers within the struct is complex since it leads to non-trivial problems. (Consider what would happen if you would move Node around in memory: How would you properly update the backpointer in NetworkManager?)
What I usually resort to is simply passing base_node as a parameter to the functions that need the backpointer. This is sometimes easier said than done, but leads to code that clearly states ownership.

Why can a struct holding a Box<T> not be copied? [duplicate]

This question already has answers here:
How do I implement Copy and Clone for a type that contains a String (or any type that doesn't implement Copy)?
(2 answers)
What is the difference between Copy and Clone?
(5 answers)
Closed 3 years ago.
An article that I read recently stated that a struct that holds a Box<T> as a field is not Copy.
This confuses me, since I thought that any type whose size is known can be stored on the stack - and is therefore Copy.
Isn't the Box's size always the same? I thought that it was just a reference to a heap allocated memory - and does therefore have always the same size.

How can I create a static String in Rust? [duplicate]

This question already has answers here:
How do I create a global, mutable singleton?
(7 answers)
Closed 3 years ago.
Is there a way I can create a static String in Rust?
I tried this:
static somestring: String = String::new();
but I got this error:
error: `std::string::String::new` is not yet stable as a const fn
--> src/main.rs:2:29
|
2 | static somestring: String = String::new();
|
How to create a static string at compile time does not solve my problem because it's about &'static str, not String. I need the String to be globally addressable.
Don't confuse the String type with the str type.
What are the differences between Rust's `String` and `str`?
A String is mutable and always heap-allocated.
A str, usually presented as &str is not mutable and is simply a view into a string.
Your question seems to confuse the idea of static and globally adressable. You may refer to (&'static str) as a string with a 'static lifetime. Strings with 'static lifetimes are common because they (mostly) represent hardcoded strings in the program. A 'static lifetime means the object (in this case, a string) will live until the end of the program. Most 'static objects are known at compile time. This should be natural to think about, because hardcoded strings are known at compile time. These strings can not mutate.
Strings on the other hand, are mutable. And you can make a String from a &'static str.
Given the lack of context in the question, if what you want is a String that is globally adressable and want to define it static I may suggest the macro lazy-static:
https://crates.io/crates/lazy_static
As sugested by mcarton, this question seems to boil down to the singleton pattern. You can learn more about its implementation in Rust here: How do I create a global, mutable singleton?

Why String objects immutable in Java? [duplicate]

This question already has answers here:
Why is String immutable in Java?
(13 answers)
Closed 7 years ago.
I'm new to java programming. And I don't understand why string objects are immutable in java.
String a = "Vehicle";
Because java uses the concept of string literal. Suppose there are five reference variables, all refers to one object "Vehicle".If one reference variable changes the value of the object, it will be affected to all the reference variables. That is why string objects are immutable in java.

Resources