+ 5
What to choose <tuple> or struct?
Let's share our thoughts about using <tuple> and struct. STRUCT. C/C++ arrays allow you to define variables that combine several data items of the same kind, but structure is another user defined data type which allows you to combine data items of different kinds. TUPLE. It is an object capable to hold a collection of elements. Each element can be of a different type. There is a lot of cool features in it, I decided to write a program for showing some of them: https://code.sololearn.com/cMEX58872LLH/#cpp Share your experience with <tuple> and struct.
1 Resposta
+ 1
tuples are anonymous types with anonymous fields
tuple structs are named types with anonymous fields
structs are named types with named fields
Let's take a look at [T]::split_at():
fn split_at(&self, mid: usize) -> (&[T], &[T])
They authors of this function choose to return a tuple here. Let's see, would we gain anything from...
naming the type: not really, right? What would we call it? SplitSlice, SliceParts, ...? Every name we could give is superfluous, because the function is already named appropriately.
naming the fields: questionable. left and right would make it more clear what part is what side. But here we assume that programmers have the correct intuition. English is written from left to right, in English cultures arrays are usually drawn from left to right ([0 | 1 | 2 | 3 ]), so it kind of makes sense for most people.
⇒ just a tuple is fine!