Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

d
d programming
tuple()
communitycreator

What are tuples in D?

Chinweuba Elijah Azubuike

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]); 
}
Example of a tuple in D programming

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.

RELATED TAGS

d
d programming
tuple()
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike
RELATED COURSES

View all Courses

Keep Exploring