4
我開始使用clippy作爲棉絨。有時,它會顯示此警告:如何從迭代器中獲取切片?
writing `&Vec<_>` instead of `&[_]` involves one more reference and cannot be
used with non-Vec-based slices. Consider changing the type to `&[...]`,
#[warn(ptr_arg)] on by default
我將參數更改爲切片,但是這會在呼叫端添加樣板。例如,代碼爲:
let names = args.arguments.iter().map(|arg| {
arg.name.clone()
}).collect();
function(&names);
,但現在卻是:
let names = args.arguments.iter().map(|arg| {
arg.name.clone()
}).collect::<Vec<_>>();
function(&names);
否則,我得到以下錯誤:
error: the trait `core::marker::Sized` is not implemented for the type
`[collections::string::String]` [E0277]
所以我想如果有一種方法來將Iterator
轉換爲slice
或避免在此特定情況下必須指定collect
ed類型。