Rust 方法存在,但未满足以下特征边界(泛型)

Rust 方法存在,但未满足以下特征边界(泛型),rust,traits,Rust,Traits,我有一个工作函数foo,它编译时不会出错: 使用chrono; fn foo(){ 让now=chrono::offset::Local::now(); 让mut buf=String::new(); buf.push_str(&now.format(“%Y/%m/%d”)。到_string(); } 当我尝试将now变量提取到参数时: fn foo(现在:chrono::DateTime){ 让mut buf=String::new(); buf.push_str(&now.format(“

我有一个工作函数
foo
,它编译时不会出错:

使用chrono;
fn foo(){
让now=chrono::offset::Local::now();
让mut buf=String::new();
buf.push_str(&now.format(“%Y/%m/%d”)。到_string();
}
当我尝试将
now
变量提取到参数时:

fn foo(现在:chrono::DateTime){
让mut buf=String::new();
buf.push_str(&now.format(“%Y/%m/%d”)。到_string();
}
我在编译时遇到以下错误:

error[E0599]: no method named `format` found for struct `chrono::DateTime<T>` in the current scope
   --> src/lib.rs:108:35
    |
108 |                 buf.push_str(&now.format("%Y/%m/%d").to_string());
    |                                   ^^^^^^ method not found in `chrono::DateTime<T>`
    |
    = note: the method `format` exists but the following trait bounds were not satisfied:
            `<T as chrono::TimeZone>::Offset: std::fmt::Display`
error[E0599]:在当前作用域中找不到结构`chrono::DateTime`名为`format`的方法
-->src/lib.rs:108:35
|
108 | buf.push_str(&now.format(“%Y/%m/%d”)。到_string();
|在“chrono::DateTime”中找不到方法`
|
=注意:方法'format'存在,但不满足以下特性界限:
`::偏移量:std::fmt::显示`
注释中说,
format
是存在的(它确实存在,就像在第一个代码片段中一样),但是特性边界没有得到满足。如何指定缺少的特性绑定以进行编译

考虑到第一个代码段是编译的,并且我只提取了与参数相同的类型,我猜这应该是可能的


相关的chrono文档:

我查看了
DateTime
的impl块。它的代码如下表所示。我更新了函数的签名以匹配,现在它已成功编译

fn foo(现在:chrono::DateTime)
哪里
T::偏移量:std::fmt::显示{
...
}