我正在使用OptsBuilder
传递一个data source name来创建一个连接池:
let mut opts = mysql::OptsBuilder::from_opts(dsn);
opts.stmt_cache_size(0);
let pool = mysql::Pool::new_manual(1, 3, opts).expect("Could not connect to MySQL");
如果我传递了无效的DSN,则代码会出现紧急情况:
thread 'main' panicked at 'URL ParseError { relative URL without a base }', ', /path/.cargo/registry/src/github.com-1ecc6299db9ec823/mysql-16.0.2/src/conn/opts.rs:827:25
如何处理错误或提前验证以防止出现恐慌?
答案 0 :(得分:0)
您可以使用from_url
创建let opts = match Opts::from_url(dsn) {
Ok(opts) => opts,
Err(e) => panic!(),// manage error
};
let mut opts = mysql::OptsBuilder::from_opts(opts);
opts.stmt_cache_size(0);
let pool = mysql::Pool::new_manual(1, 3, opts).expect("Could not connect to MySQL");
,以确保之后不会失败:
{{1}}