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 to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources