【发布时间】:2015-10-30 15:30:33
【问题描述】:
我正在尝试使用selection_sort 创建一个已排序的向量,同时保留原始未排序的向量:
fn main() {
let vector_1: Vec<i32> = vec![15, 23, 4, 2, 78, 0];
let sorted_vector = selection_sort(&vector_1);
println!("{:?} is unsorted, \n{:?} is sorted.", &vector_1, &sorted_vector);
}
fn selection_sort(vector_1: &Vec<i32>) -> Vec<i32> {
let mut vector = vector_1;
let start = 0;
while start != vector.len() {
for index in (start .. vector.len()) {
match vector[index] < vector[start] {
true => vector.swap(index, start),
false => println!("false"), // do nothing
}
}
start += 1;
}
vector
}
错误:
Compiling selection_sort v0.1.0 (file:///home/ranj/Desktop/Rust/algorithms/sorting/selection_sort)
src/main.rs:21:5: 21:11 error: mismatched types:
expected `collections::vec::Vec<i32>`,
found `&collections::vec::Vec<i32>`
(expected struct `collections::vec::Vec`,
found &-ptr) [E0308]
src/main.rs:21 vector
^~~~~~
src/main.rs:21:5: 21:11 help: run `rustc --explain E0308` to see a detailed explanation
error: aborting due to previous error
Could not compile `selection_sort`.
【问题讨论】:
标签: rust type-mismatch