Bill
Bill

Reputation: 5150

() cannot be formatted with the default formatter

mystring.rs

pub fn return_string() {
  return "Some String"
}

then in main, I want to print this string

mod mystring;
const test = config::return_string();
println!("{}", test);

the error I get is

println!("{}", test);
   |                    ^^^^ `()` cannot be formatted with the default formatter

Upvotes: 5

Views: 14297

Answers (1)

Finomnis
Finomnis

Reputation: 22686

I assume that your minimal reproducible example is:

pub fn return_string() {
    return "Some String"
}

fn main() {
    const test = return_string();
    println!("{}", test);
}
error: missing type for `const` item
 --> src/main.rs:6:11
  |
6 |     const test = return_string();
  |           ^^^^ help: provide a type for the constant: `test: ()`

error[E0308]: mismatched types
 --> src/main.rs:2:12
  |
1 | pub fn return_string() {
  |                        - help: try adding a return type: `-> &'static str`
2 |     return "Some String"
  |            ^^^^^^^^^^^^^ expected `()`, found `&str`

error[E0277]: `()` doesn't implement `std::fmt::Display`
 --> src/main.rs:7:20
  |
7 |     println!("{}", test);
  |                    ^^^^ `()` cannot be formatted with the default formatter
  |
  = help: the trait `std::fmt::Display` is not implemented for `()`
  = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
  = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)

Solution

There are two errors in your code:

  • don't use const inside of functions. Use let. let is an immutable value. let mut would be mutable. const is only used for immutable globals.
  • you are missing the return type for your return_string() function

I'll assume here that the return type is &str, but it might as well have to be String. For more info search for &str vs String.

Third, as a minor annotation, avoid return as much as possible, if not required. The last line of a function is automatically the return type if you don't finish it with ;.

pub fn return_string() -> &'static str {
    "Some String"
}

fn main() {
    let test = return_string();
    println!("{}", test);
}
Some String

Explanation of the error message

The error message says that () is not printable.

() is the empty type, analogous to void in C++. As you don't annotate the return type of return_string(), Rust assumes it's (). And () cannot be printed directly, at least not with the Display formatter.

You could print it with the Debug formatter, though:

pub fn return_void() {}

fn main() {
    let test = return_void();
    println!("{:?}", test);
}
()

Note that contrary to C++, () is actually a storable type, even if it is of size 0 with no data in it. That makes things a lot easier for generics. C++ templates that need to be able to deal with void return values were a major pain factor for me in the past, as they always required a special case.

Upvotes: 4

Related Questions