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)
Related
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
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.
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.
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.
This question already has answers here:
How can I align a struct to a specified byte boundary?
(2 answers)
Closed 4 years ago.
I want to write a Rust FFI for a C struct using the aligned attribute.
On nightly, one can use #[feature(repr_simd)] as in this question. The same technique without #[repr(simd)] appears to be restricted to a maximum alignment of 8 bytes.
There are various issues and RFCs open for both alignment and SIMD, and the compiler points to tracking issue #27731 which seems to be stalled.
RFC #325 points pretty clearly to no, but it is somewhat old.
Is it possible to do this with the stable compiler, in pure (unsafe?) Rust as of version 1.22?
As of now, the answer is yes, you may specify a type's alignment in stable Rust. This was stabilized in 1.25.0. It is documented under the reference's Type Layout section. Note that the alignment must be a power of 2, you may not mix align and packed representations, and aligning a type may add extra padding to the type. Here's an example of how to use the feature:
#[repr(align(64))]
struct S(u8);
fn main() {
println!("size of S: {}", std::mem::size_of::<S>());
println!("align of S: {}", std::mem::align_of::<S>());
}