What are tuples in D?
Overview
In D, tuples are data structures that can hold different types of data elements, which includes expressions, types, aliases, etc.
The tuple() method is used to create a tuple.
Example
Let's see an example:
import std.stdio;import std.typecons;void main() {auto stuple = tuple(1, "learn");writeln(stuple);writeln(stuple[0]);writeln(stuple[1]);}
Explanation
-
Line 5: We declare a tuple variable
stupleand assign two data elements,1andlearn, to it. -
Line 6: We print the
stuple, which shows us the type of data in our tuple and the tuple values. -
Line 7: We use the
0index to get the tuple’s first value,1. -
Line 8: We use the
1index to get the tuple’s second value,learn.