Variables and values
This topic is not available in the selected language or version. Showing the start of this section.
Use let to give a value a name. Use let mut if you will change it later. An annotation such as : Int specifies a type; omit it when the value makes the type clear.
Save this as variables.xen:
fn main() { let price: Int = 12; let mut quantity = 2; quantity = quantity + 1; let total = price * quantity; println(total); println(float(total) / 2.0);}$ xen run variables.xen3618.0Changing quantity does not automatically update a previously calculated total. Calculate again to get a new total. Assigning a new value to a variable declared with let is an error.
Everyday types
Section titled “Everyday types”| Type | Values |
|---|---|
Int | Integers such as 42 and -7; the same type as I64 |
Float | Numbers such as 2.5; the same type as F64 |
Bool | true and false |
String | Strings such as "hello" |
Unit | The return type of a function with no result; its value is () |
Start with Int for whole numbers, Float for fractions, Bool for conditions, and String for text. Omit the annotation when the value makes the type clear, as with quantity above.
Arithmetic and conversion
Section titled “Arithmetic and conversion”Use +, -, *, /, and integer remainder %. Dividing two Int values produces an integer. Convert with float(...) first if you need a fractional result.
Division by zero causes a runtime error. The next chapter covers numeric widths, ranges, and conversion between different types.
Comparisons and comments
Section titled “Comparisons and comments”==, !=, <, <=, >, and >= produce a Bool. Combine conditions with &&, ||, and !. && and || skip the right operand when the left operand determines the answer. Write a line comment after //.
You cannot declare the same name twice in one block. A nested { ... } block may declare a name used outside it; the outer name becomes visible again after leaving the block.
The next chapter, Data types and conversions, develops type selection, conversion failures, and the boundaries of inference through worked examples.