Learn D - 10 Code Examples & CST Typing Practice Test
D is a high-level, statically typed, compiled systems programming language combining C-like performance with modern features like garbage collection, functional programming, and meta-programming.
Learn D with Real Code Examples
Updated Nov 20, 2025
Code Sample Descriptions
D Counter and Theme Toggle
import std.stdio;
int count = 0;
bool isDark = false;
void updateUI() {
writeln("Counter: ", count);
writeln("Theme: ", isDark ? "Dark" : "Light");
}
void increment() {
count += 1;
updateUI();
}
void decrement() {
count -= 1;
updateUI();
}
void reset() {
count = 0;
updateUI();
}
void toggleTheme() {
isDark = !isDark;
updateUI();
}
// Simulate actions
updateUI();
increment();
increment();
toggleTheme();
decrement();
reset();
Demonstrates a simple counter with theme toggling using D variables and console output.
D Simple Addition
import std.stdio;
void main() {
int a = 10;
int b = 20;
int sum = a + b;
writeln("Sum: ", sum);
}
Adds two numbers and prints the result.
D Factorial
import std.stdio;
int factorial(int n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
void main() {
writeln("Factorial 5: ", factorial(5));
}
Calculates factorial recursively.
D Fibonacci Sequence
import std.stdio;
int fib(int n) {
return n < 2 ? n : fib(n - 1) + fib(n - 2);
}
void main() {
foreach(i; 0 .. 10)
writeln(fib(i));
}
Generates first 10 Fibonacci numbers.
D Max of Two Numbers
import std.stdio;
void main() {
int a = 10, b = 20;
int max = a > b ? a : b;
writeln("Max: ", max);
}
Finds the maximum of two numbers.
D Array Sum
import std.stdio;
void main() {
int[] arr = [1,2,3,4,5];
int sum = 0;
foreach(x; arr)
sum += x;
writeln("Sum: ", sum);
}
Sums elements of an array.
D Even Numbers Filter
import std.stdio;
void main() {
int[] arr = [1,2,3,4,5];
foreach(x; arr)
if(x % 2 == 0)
writeln(x);
}
Prints even numbers from an array.
D Conditional Counter Increment
import std.stdio;
void main() {
int count = 3;
if(count < 5)
count++;
writeln("Count: ", count);
}
Increment counter only if less than 5.
D Resettable Counter
import std.stdio;
void main() {
int count = 0;
count += 1;
count += 1;
writeln("Count: ", count);
count = 0;
writeln("Count: ", count);
}
Counter that increments and can be reset.
D Theme Toggle Only
import std.stdio;
void main() {
bool isDark = false;
writeln("Theme: ", isDark ? "Dark" : "Light");
isDark = !isDark;
writeln("Theme: ", isDark ? "Dark" : "Light");
isDark = !isDark;
writeln("Theme: ", isDark ? "Dark" : "Light");
}
Toggles theme multiple times.
Frequently Asked Questions about D
What is D?
D is a high-level, statically typed, compiled systems programming language combining C-like performance with modern features like garbage collection, functional programming, and meta-programming.
What are the primary use cases for D?
Systems programming and OS-level development. High-performance computing. Game engines and graphics programming. Financial and trading applications. Compile-time code generation and metaprogramming
What are the strengths of D?
High-performance native code. Clean modern syntax with C-style familiarity. Powerful compile-time metaprogramming. Versatile multi-paradigm language. Rich standard library with ranges and algorithms
What are the limitations of D?
Smaller community and ecosystem than C++ or Rust. Less industrial adoption for large-scale projects. Limited GUI library support. Garbage collector can be unpredictable in real-time systems. Interfacing with C++ can be complex
How can I practice D typing speed?
CodeSpeedTest offers 10+ real D code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.