我try 从the mysql
crate's documentation的基本示例开始,但使用的是格式化字符串,而不是完全硬编码的字符串:
use mysql::*;
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
// These will be read from a config file
let user = "root";
let pw = "password";
let host = "localhost";
let port = 3307;
let db = "db_name";
let url = format!("mysql://{user}:{pw}@{host}:{port}/{db}");
let pool = Pool::new(url)?; // Error at this line
let mut conn = pool.get_conn()?;
Ok(())
}
但是,我得到了这个错误:
error[E0277]: the trait bound `Opts: From<std::string::String>` is not satisfied
--> src/main.rs:13:23
|
13 | let pool = Pool::new(url)?;
| --------- ^^^ the trait `From<std::string::String>` is not implemented for `Opts`
| |
| required by a bound introduced by this call
我是铁 rust 新手,整个晚上都在寻找解决方案.为什么format!
的输出不执行From<String>
?如何将格式化的字符串而不是硬编码的字符串传递给Pool::new
?谢谢