无法使用默认格式进行格式化

Bil*_*ill 6 rust

mystring.rs

pub fn return_string() {
  return "Some String"
}
Run Code Online (Sandbox Code Playgroud)

然后在 main 中,我想打印这个字符串

mod mystring;
const test = config::return_string();
println!("{}", test);
Run Code Online (Sandbox Code Playgroud)

我得到的错误是

println!("{}", test);
   |                    ^^^^ `()` cannot be formatted with the default formatted
Run Code Online (Sandbox Code Playgroud)

Fin*_*nis 3

假设您的最小可重现示例是:

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

fn main() {
    const test = return_string();
    println!("{}", test);
}
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)

解决方案

您的代码中有两个错误:

  • 不要const在函数内部使用。使用letlet 一个不可变的值。let mut会是可变的。const仅用于不可变的全局变量。
  • 您缺少return_string()函数的返回类型

我在这里假设返回类型是&str,但也可能必须是String。欲了解更多信息,请搜索&str vs String.

第三,作为次要注释,return如果不需要,请尽可能避免。如果您不使用 结束函数,则函数的最后一行将自动成为返回类型;

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)
Run Code Online (Sandbox Code Playgroud)
Some String
Run Code Online (Sandbox Code Playgroud)

错误消息的解释

错误消息表明该内容()不可打印。

()是空类型,类似于voidC++ 中的类型。由于您没有注释 的返回类型return_string(),Rust 假定它是()。并且()不能直接打印,至少不能用Display格式化程序。

Debug不过,您可以使用格式化程序打印它:

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

fn main() {
    let test = return_string();
    println!("{}", test);
}
Run Code Online (Sandbox Code Playgroud)
()
Run Code Online (Sandbox Code Playgroud)

请注意,与 C++ 相反,()它实际上是一种可存储类型,即使它的大小很大0且其中没有数据。这使得仿制药的事情变得容易得多void过去,需要能够处理返回值的 C++ 模板对我来说是一个主要的痛苦因素,因为它们总是需要特殊情况。