Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Struct

Part of TutorialFundamentals

Description

Structs allow you to bind together basic data types into a more complicated paradigm.

Example

import std.stdio;

struct Vehicle
{  
    string name;
    double cost;
    int wheels;
}

void printVehicleInfo(Vehicle vehicle)
{
    writefln("vehicle name: %s", vehicle.name);
    writefln("vehicle cost: %s", vehicle.cost);
    writefln("vehicle wheels: %s\n", vehicle.wheels);
}

void main()
{      
    Vehicle car;
    car.name = "Audi";
    car.cost = 20_000;
    car.wheels = 4;
    
    Vehicle bike;
    bike.name = "Harley";
    bike.cost = 2_000;
    bike.wheels = 2;
    
    printVehicleInfo(car);
    printVehicleInfo(bike);
}

Source

Partially based on struct.html by jcc7.