Every unit Stof supports, for looking things up. Types & Units covers how units work. Each unit's abbreviation works after a number (5ft, 20kg), and either the abbreviation or the full name works as a type:
#[main]
fn main() {
const a: km = 3km;
const b: kilometers = 3km;
pln(a == b);
pln((135cm + 5ft + 123in as m).round(4));
}
When two different units of the same kind combine, the result is in the larger of the two (500m + 1km is in km). Mixed angles always give radians.
Length
| Unit | Full Name |
|---|
km | kilometers |
hm | hectometers |
dcm | decameters |
m | meters |
dm | decimeters |
cm | centimeters |
mm | millimeters |
um | micrometers |
nm | nanometers |
mi | miles |
yd | yards |
ft | feet |
in | inches |
Mass
| Unit | Full Name |
|---|
Gt | gigatonnes |
Mt | megatonnes |
t | tonnes |
kg | kilograms |
g | grams |
mg | milligrams |
ug | micrograms |
ng | nanograms |
pg | picograms |
Ton | tons |
lb | lbs (imperial pounds) |
oz | ounces |
Time
| Unit | Full Name |
|---|
day | days |
hr | hours |
min | minutes |
s | seconds |
ms | milliseconds |
us | microseconds |
ns | nanoseconds |
Time::now() returns milliseconds, so adding or subtracting any other time unit works directly. The result is in the larger unit (Time::now() + 1hr is in hours), so before comparing it with a plain number of milliseconds (Ex. a time from JSON), give the number its units: const at: ms = record.expires;.
Temperature
| Unit | Full Name |
|---|
K | kelvin |
C | celsius |
F | fahrenheit |
Angles
| Unit | Full Name | Range |
|---|
rad | radians | clamped to ±360° equivalent |
deg | degrees | clamped to ±360° |
prad | pradians (positive radians) | clamped to [0°, 360°) |
pdeg | pdegrees (positive degrees) | clamped to [0°, 360°) |
The positive versions keep angles between 0 and a full turn: casting -90deg to pdeg gives 270pdeg.
Memory (Decimal)
| Unit | Full Name |
|---|
bit / bits | |
byte / bytes | |
KB | kilobytes |
MB | megabytes |
GB | gigabytes |
TB | terabytes |
PB | petabytes |
EB | exabytes |
ZB | zettabytes |
YB | yottabytes |
Memory (Binary)
| Unit | Full Name |
|---|
KiB | kibibytes |
MiB | mebibytes |
GiB | gibibytes |
TiB | tebibytes |
PiB | pebibytes |
EiB | exbibytes |
ZiB | zebibytes |
YiB | yobibytes |
These are two different sets of units: MB is 1,000,000 bytes and MiB is 1,048,576, and the gap grows with size. When something says GB but means GiB, as is common in computing, cast to the unit it actually means.