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

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.

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

How does String::from("") & "".to_string() differ in Rust? [duplicate]

This question already has answers here:
How to create a String directly?
(3 answers)
What is the difference between these 3 ways of declaring a string in Rust?
(1 answer)
Closed 2 years ago.
How does String::from("") & "".to_string() differ in Rust?
Is there any difference in stack and heap allocation in both cases?
How does String::from("") & "".to_string() differ in Rust?
They're part of different protocols (traits): std::convert::From and alloc::string::ToString[0].
However, when it comes to &str/String they do the same thing (as does "".to_owned()).
Is there any difference in stack and heap allocation in both cases?
As joelb's link indicates, before Rust 1.9 "".to_string() was markedly slower than the alternatives as it went through the entire string formatting machinery. That's no longer the case.
[0] `ToString` is also automatically implemented if the structure implements `Display`[1]
[1] functionally s.to_string() is equivalent to format!("{}", s), it's usually recommended to not implement ToString directly, unless bypassing the formatting machinery can provide significant performance improvements (which is why str/String do it)

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.

How to create a minimum BinaryHeap? [duplicate]

This question already has answers here:
How do I create a BinaryHeap that pops the smallest value, not the largest?
(2 answers)
Closed 3 years ago.
I'm trying to solve leetcode problem 703, largest_element_in_a_stream in Rust.
I want to use the BinaryHeap to solve this problem, but the BinaryHeap in Rust is the maximum heap by default. I don't know how to transform it to a maximum heap.
I found answers in similar questions:
How do I create a BinaryHeap that pops the smallest value, not the largest?
How can I implement a min-heap of f64 with Rust's BinaryHeap?
But the answer in the two questions uses some special struct and overloads the Ord trait, I want to solve it for primitives such as i32.
How can I solve it?
Assuming you actually want a min-heap, you could just negate each value you put it in the heap & negate each you take out.
Note: As #Shepmaster alludes to, there is a single i32 negative value which does not have a corresponding positive one (to balance 0, which is its own negative). If you need to handle this value, this technique will not work, at least not without a bit of finessing.

Global constant initialized at runtime in Rust? [duplicate]

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.

Resources