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 stuple and assign two data elements, 1 and learn, 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 0 index to get the tuple’s first value, 1.

  • Line 8: We use the 1 index to get the tuple’s second value, learn.

New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources