rust custom indexable data types [duplicate] - rust

This question already has answers here:
Is there a way to perform an index access to an instance of a struct?
(2 answers)
Closed 4 years ago.
Is it possible to create custom data types in rust that can be indexed like Vec and Slice? If yes which Traits do I need to implement?
Example for indexed access:
let mut vec = vec![1, 2, 3];
assert_eq!(vec[1], 2);

Yes, the traits are called Index and IndexMut.

Related

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.

Can a struct implement two traits with conflicting method names in Rust? [duplicate]

This question already has answers here:
How do I disambiguate traits in Rust?
(2 answers)
How to call a method when a trait and struct use the same method name?
(1 answer)
Closed 4 years ago.
Is it possible to implement two traits with conflicting method names in Rust? I know that it gives you a multiple applicable methods in scope error, but is there a way to resolve this? For example, some languages handle multiple inheritance by allowing you to explicitly specify which one method should take precedence
You want universal function call syntax. The following are all equivalent:
let v = 32;
let _ = v.clone();
let _ = Clone::clone(&v);
let _ = <i32 as Clone>::clone(&v);

How do I create a polymorphic array and then convert a value to the concrete type? [duplicate]

This question already has answers here:
Vector of objects belonging to a trait
(3 answers)
How to get a reference to a concrete type from a trait object?
(2 answers)
Can I do type introspection with trait objects and then downcast it?
(2 answers)
Closed 5 years ago.
I'm rewriting a C++ application in Rust and I need to implement an array of pointers to the base class and populate the array with derived classes.
The C++:
BaseClass base[2] = new BaseClass[2];
base[0] = FirstDerivedClass ();
base[1] = SecondDerivedClass ();
FirstDerivedClass *fderived = dynamic_cast<FirstDerivedClass> (base[0]);
if (fderived != nullptr) {
fderived.exclusive_method ();
}
I've tried to create something similar using Vec<Box<BaseTrait>> but there is no way to cast it to the appropriate derived class.
A solution with enum is not appropriate as there are great differences in size between the variants and I need to allocate several thousands of elements. This led me to combine a struct boxed in enum, but I don't know how to implement it.

How to swap non-copyable elements in a vector? [duplicate]

This question already has an answer here:
How to swap the elements of an array, slice, or Vec?
(1 answer)
Closed 5 years ago.
I would like to swap String elements of a vector
let mut v_sa: Vec<String> = vec![];
...
let temp_a = v_sa_ib[j];
v_sa_ib[j] = v_sa_ib[j+1];
v_sa_ib[j+1] = temp_a;
It works with integers but not with Strings. I also tried to create a second vector temp_b (same type of temp_a) and to populate it instead of swapping but i got always the same error: the type of this value must be known in this context.
How can I fix it?
You can use the swap() method of Vec to swap two elements directly:
v_sa_ib(j, j+1);
There is also a generic swap function called std::mem::swap but it is difficult to use here because obtaining two mutable references two different elements of the same Vec is quite involved.
The reason why it worked with integers is that integers implement the Copy trait, so Rust can automatically create new copies.

Are reference values copied in Rust? [duplicate]

This question already has answers here:
Copy/move semantics documentation of &T/&mut T types itself
(2 answers)
Closed 5 years ago.
Am I correct to assume that, for the following code
let a = vec![1, 2, 3];
let b = &a;
let c = b;
The memory presentation will be something like this, assuming the value of b is "B"?
_ _
b|B| c|B|
|____________|
|
V
_________
a|_________|
I'm only asking about immutable references, as there can be only 1 mutable reference, as far as I remember.
Yes, this is correct.
In Rust terms, &T is Copy, which means that it can be copied bitwise without transferring ownership.

Resources