Operators
List of all supported operators ordered by precedence.
Precedence affects the order how things are evaluated. The most basic example is a * b + c * d
in which case you want the parser to interpret it like (a * b) + (c * d)
and not like a * (b + (c * d))
(or some other variation).
Prefix: -
, !
-
prefix (e.g.-a
) is a negation, which can be done onint
,float
,ksp::math::Vec2
,ksp::math::Vec3
!
prefix (e.g.!condition
) is a logical not, which can be done onbool
Power of (version >= 0.4.3)
**
(e.g.a ** b
) raisesa
to the power ofb
int ** int
resultint
float ** float
resultfloat
Multiplication / division / modulo: *
, /
, %
*
(e.g.a * b
) multiplies two values, which is allowed forint * int
resultint
float * float
resultfloat
float * ksp::math::Vec2
resultksp::math::Vec2
ksp::math::Vec2 * float
resultksp::math::Vec2
ksp::math::Vec2 * ksp::math::Vec2
resultfloat
(vector dot product)float * ksp::math::Vec3
resultksp::math::Vec3
ksp::math::Vec3 * float
resultksp::math::Vec3
ksp::math::Vec3 * ksp::math::Vec3
resultfloat
(vector dot product)Note: You can not multiply
int
withfloat
, one of them as to be converted via.to_float
or.to_int
/
(e.g.a / b
) divides first value by the second value, which is allowed forint / int
resultint
(Note: result will be truncated1 / 2
is0
)float / float
resultfloat
ksp::math::Vec2 / float
resultksp::math::Vec2
ksp::math::Vec3 / float
resultksp::math::Vec3
Note: You can not device
int
withfloat
or vice versa, one of them as to be converted via.to_float
or.to_int
%
(e.g.a % b
) gets the remainder of the division of the first value by the second value, which can on, which is allowed forint % int
resultint
float % float
resultfloat
Addition / subtraction: +
, -
+
(e.g.a + b
) adds two values, which is allowed forint + int
resultint
float + float
resultfloat
string + string
resultstring
(concatenate two strings)ksp::math::Vec2 + ksp::math::Vec2
resultksp::math::Vec2
ksp::math::Vec3 + ksp::math::Vec3
resultksp::math::Vec3
Note: You can not add
int
withfloat
, one of them as to be converted via.to_float
or.to_int
-
(e.g.a - b
) subtracts the second value from the first, which is allowed forint - int
resultint
float - float
resultfloat
ksp::math::Vec2 - ksp::math::Vec2
resultksp::math::Vec2
ksp::math::Vec3 - ksp::math::Vec3
resultksp::math::Vec3
Note: You can not subtract
int
withfloat
, one of them as to be converted via.to_float
or.to_int
Bit-And / Bit-Or / Bit-Xor: &
, |
, ^
Create range: ..
, ...
Ranges can be created from two int
value.
a .. b
creates a range froma
tob
exclusively (i.e.>= a
and< b
)a ... b
creates a range froma
tob
inclusively (i.e.>= a
and<= b
)
Comparison: ==
, !=
, <
, <=
, >
, >=
The result of a comparison is always a
bool
==
and!=
check the equality resp. not-equality of two values. The following types can be comparedbool
withbool
int
withint
float
withfloat
string
withstring
ksp::math::Vec2
withksp::math::Vec2
ksp::math::Vec3
withksp::math::Vec3
<
,<=
,>
,>=
compare the order of two values, i.e. less than, less of equal, greater than, greater or equal. The following types can be comparedint
withint
float
withfloat
string
withstring
(standard string ordering)
Boolean and / Boolean or: &&
, ||
&&
combines twobool
with an and||
combines twobool
with an or
Note that this operator short-circuits if the result is already determined by the first boolean. E.g. if you have a boolean value and a function returning a boolean:
fn some_func() -> bool = { ... }
let a : bool
then a && some_func()
will not invoke some_func
if a
is false
. Correspondingly a || some_func()
will not invoke some_func
if a
is `true.