pal*_*dot 5 printing list elixir
我想打印一个列表以及字符串标识符
list = [1, 2, 3]
IO.puts "list is ", list
Run Code Online (Sandbox Code Playgroud)
这不起作用.我尝试过几个不同的变化
# this prints only the list, not any strings
IO.inspect list
# using puts which also does not work
IO.puts "list is #{list}"
Run Code Online (Sandbox Code Playgroud)
在javascript中,我可以做到console.log("list is ", list)
.我很困惑如何在灵药中实现同样的目标.
Ale*_*kin 12
从Elixir 1.4开始,IO.inspect/2
接受其他label
选项:
IO.inspect list, label: "The list is"
#? The list is: [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
也许有更好的方法(我也是 Elixir 的新手),但这对我有用:
IO.puts(["list is ", Enum.join(list, " ")])
list is 1 2 3
Run Code Online (Sandbox Code Playgroud)
插值也有效:
IO.puts("list is #{Enum.join(list, " ")}")
Run Code Online (Sandbox Code Playgroud)
编辑: 对于这个用例,inspect似乎比Enum.join更好:
IO.puts("list is #{inspect(list)}")
list is [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5359 次 |
最近记录: |