Hello World in Swift

Our first program will print the classic “hello world” message. Here’s the full source code.

import Foundation

print("hello world")

To run the program, save the code in a file with a .swift extension and use the Swift compiler to run it.

$ swift hello-world.swift
hello world

Sometimes we’ll want to build our programs into binaries. We can do this using the Swift compiler.

First, compile the code into an executable:

$ swiftc hello-world.swift -o hello-world
$ ls
hello-world  hello-world.swift

We can then execute the built binary directly.

$ ./hello-world
hello world

Now that we can run and build basic Swift programs, let’s learn more about the language.