IO.puts vs IO.inspect

It seems to me that IO.puts and IO.inspect are used for printing on the console. What is the difference between the two?

+5
source share
2 answers

Adding to the previous answer, IO.inspect can print an arbitrary elixir member with an optional keyword list containing label: and the values ​​for initializing Inspect.Opts struct:

 @spec inspect(item, Keyword.t) :: item when item: var 

IO.puts requires the argument to be either a string or a structure that implements the String.Chars protocol:

 @spec puts(device, chardata | String.Chars.t) :: :ok 
+5
source

Reading Elixir documents looks like IO.puts/2 just about to write and add a new line.

IO.inspect/2 will do the same, but it also returns the first value unchanged (so it can be chain-bound), it allows pretty print / decoration and other formatting options.

A friendly reminder that hexdocs can be really awesome. I could easily find the answer to your question and study the differences myself. I highly recommend that you read the modules you usually use to discover other features that you may not know about that you could use.

+3
source

Source: https://habr.com/ru/post/1269048/


All Articles