Learn X By Example · Elixir 篇 · Values in Elixir
Elixir有丰富的值类型,包括 字符串,整数,浮点数,布尔值等等。下面是一些基本的例子。
defmodule Values do
def main do
# Strings, which can be concatenated with <>
IO.puts("elixir" <> "lang")
# Integers and floats
IO.puts("1+1 = #{1 + 1}")
IO.puts("7.0/3.0 = #{7.0 / 3.0}")
# Booleans, with boolean operators as you'd expect
IO.puts(true and false)
IO.puts(true or false)
IO.puts(not true)
end
end
Values.main()
将代码保存为 values.exs
并使用 elixir
命令运行:
$ elixir values.exs
elixirlang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false
在 Elixir 中,我们通常不是将程序编译成单独的二进制文件。而是直接使用 elixir
命令运行,或者做为 Mix 管理的更大工程的一部分。
这个示例说明了Elixir中的基本值类型,包括 字符串,数字,和布尔值。注意,Elixir 使用 <>
来拼接字符串,使用 and
,or
,not
进行布尔运算,以及 #{...}
用于字符串插值。