Skip to content

Commit

Permalink
Refactor to mention print method
Browse files Browse the repository at this point in the history
  • Loading branch information
grasshopper47 committed Nov 30, 2023
1 parent e7f217d commit 9ccc11a
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions docs/docs/standard_library/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ keywords:
[
noir logging,
println statement,
print statement,
debugging in noir,
noir std library,
logging tutorial,
Expand All @@ -17,14 +18,13 @@ keywords:
]
---

The standard library provides a familiar `println` statement you can use. Despite being a limited
implementation of rust's `println!` macro, this construct can be useful for debugging.
The standard library provides two familiar statements you can use: `println` and `print`. Despite being a limited implementation of rust's `println!` and `print!` macros, these constructs can be useful for debugging.

You can print the output of println statements in your Noir code by using the `nargo execute` command or the `--show-output` flag when using `nargo test` (provided there are println statements in your tests).
You can print the output of both statements in your Noir code by using the `nargo execute` command or the `--show-output` flag when using `nargo test` (provided there are print statements in your tests).

It is recommended to use `nargo execute` if you want to debug failing constrains with `println` statements. This is due to every input in a test being a constant rather than a witness, so we issue an error during compilation while we only print during execution (which comes after compilation). `println` will not work for failed constraints caught at compile time.
It is recommended to use `nargo execute` if you want to debug failing constrains with `println` or `print` statements. This is due to every input in a test being a constant rather than a witness, so we issue an error during compilation while we only print during execution (which comes after compilation). Neither `println`, nor `print` are callable for failed constraints caught at compile time.

The `println` statement is unconstrained, so it works for outputting integers, fields, strings, and even structs or expressions. For example:
Both `print` and `println` statements have the `unconstrained` attribute, enabling them to output integers, fields, strings, and even structs or expressions. For example:

```rust
use dep::std;
Expand All @@ -40,10 +40,9 @@ fn main(age : Field, height : Field) {
std::println(age + height);
std::println("Hello world!");
}

```

You can print multiple different types in the same statement and string as well as a new "fmtstr" type. A `fmtstr` can be specified in the same way as a normal string it just should be prepended with an "f" character:
You can print different types in the same statement (including strings) with a type called `fmtstr`. It can be specified in the same way as a normal string, just prepended with an "f" character:

```rust
let fmt_str = f"i: {i}, j: {j}";
Expand All @@ -59,4 +58,20 @@ You can print multiple different types in the same statement and string as well

let foo = fooStruct { my_struct: s, foo: 15 };
std::println(f"s: {s}, foo: {foo}");

std::println(15); // prints 0x0f, implicit Field
std::println(-1 as u8); // prints 255
std::println(-1 as i8); // prints -1
```

Examples shown above are interchangeable between the two `print` statements:

```rust
let person = Person { age : age, height : height };

std::println(person);
std::print(person);

std::println("Hello world!"); // Prints with a newline at the end of the input
std::print("Hello world!"); // Prints the input and keeps cursor on the same
```

0 comments on commit 9ccc11a

Please sign in to comment.