It seems to me that IO.puts
and IO.inspect
are both used to print to the console. What is the difference between them?
Adding to the previous answer, IO.inspect
can print an arbitrary elixir term, with an optional keyword list containing a label:
and values for initializing an Inspect.Opts struct:
@spec inspect(item, Keyword.t) :: item when item: var
IO.puts
requires the argument to be either a string, or a struct that implements the String.Chars
protocol:
@spec puts(device, chardata | String.Chars.t) :: :ok
Reading through the Elixir docs, it looks like IO.puts/2
is simply going to write and append a newline.
IO.inspect/2
will do the same thing, but it also returns the first value unchanged (so it's chainable), enables pretty printing/decoration and other formatting options.
Friendly reminder that hexdocs can be really awesome. I was able to easily find the answer to your question and learn the differences myself. I highly encourage you to read through modules you normally use to discover other functions you might not know about that you could be benefitting from.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With