我试图在Rust中实现一个冒泡排序算法,但是我陷入了类型不匹配的错误。有人可以协助执行吗?
同样,它的实现方式与我在Python中实现的方式相同。我敢肯定有一种乡村的方式来实现这一点。
fn main() {
let mut list = [15, 3, 2, 1, 6, 0];
bubble_sort(list);
println!("order list is: {:?}", &list);
}
fn bubble_sort(list: &mut [usize]) {
for i in 0..&list.len() {
for j in 0..(&list.len()-1) {
if &list[&j] > &list[&j+1] {
&list.swap( &list[&j], &list[&j+1] );
}
}
}
}
编译器错误:
Compiling bubble_sort v0.1.0 (file:///home/ranj/Desktop/Rust/algorithms/sorting/bubble_sort)
src/main.rs:5:17: 5:21 error: mismatched types:
expected `&mut [usize]`,
found `[_; 6]`
(expected &-ptr,
found array of 6 elements) [E0308]
src/main.rs:5 bubble_sort(list);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
src/main.rs:5:17: 5:21 help: run `rustc --explain E0308` to see a detailed explanation
src/main.rs:11:14: 11:30 error: start and end of range have incompatible types: expected `_`, found `&usize` (expected integral variable, found &-ptr) [E0308]
src/main.rs:11 for i in 0..&list.len() {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
note: in expansion of for loop expansion
src/main.rs:11:5: 17:6 note: expansion site
src/main.rs:11:14: 11:30 help: run `rustc --explain E0308` to see a detailed explanation
src/main.rs:13:17: 13:25 error: the trait `core::ops::Index<&usize>` is not implemented for the type `[usize]` [E0277]
src/main.rs:13 if &list[&j] > &list[&j+1] {
~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
note: in expansion of for loop expansion
src/main.rs:12:9: 16:10 note: expansion site
note: in expansion of for loop expansion
src/main.rs:11:5: 17:6 note: expansion site
src/main.rs:13:17: 13:25 note: the type `[usize]` cannot be indexed by `&usize`
src/main.rs:13 if &list[&j] > &list[&j+1] {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
note: in expansion of for loop expansion
src/main.rs:12:9: 16:10 note: expansion site
note: in expansion of for loop expansion
src/main.rs:11:5: 17:6 note: expansion site
src/main.rs:13:17: 13:25 error: the trait `core::ops::Index<&usize>` is not implemented for the type `[usize]` [E0277]
src/main.rs:13 if &list[&j] > &list[&j+1] {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
note: in expansion of for loop expansion
src/main.rs:12:9: 16:10 note: expansion site
note: in expansion of for loop expansion
src/main.rs:11:5: 17:6 note: expansion site
src/main.rs:13:17: 13:25 note: the type `[usize]` cannot be indexed by `&usize`
src/main.rs:13 if &list[&j] > &list[&j+1] {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
note: in expansion of for loop expansion
src/main.rs:12:9: 16:10 note: expansion site
note: in expansion of for loop expansion
src/main.rs:11:5: 17:6 note: expansion site
src/main.rs:14:30: 14:38 error: the trait `core::ops::Index<&usize>` is not implemented for the type `[usize]` [E0277]
src/main.rs:14 &list.swap( &list[&j], &list[&j+1] );
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
note: in expansion of for loop expansion
src/main.rs:12:9: 16:10 note: expansion site
note: in expansion of for loop expansion
src/main.rs:11:5: 17:6 note: expansion site
src/main.rs:14:30: 14:38 note: the type `[usize]` cannot be indexed by `&usize`
src/main.rs:14 &list.swap( &list[&j], &list[&j+1] );
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
note: in expansion of for loop expansion
src/main.rs:12:9: 16:10 note: expansion site
note: in expansion of for loop expansion
src/main.rs:11:5: 17:6 note: expansion site
src/main.rs:14:30: 14:38 error: the trait `core::ops::Index<&usize>` is not implemented for the type `[usize]` [E0277]
src/main.rs:14 &list.swap( &list[&j], &list[&j+1] );
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
note: in expansion of for loop expansion
src/main.rs:12:9: 16:10 note: expansion site
note: in expansion of for loop expansion
src/main.rs:11:5: 17:6 note: expansion site
src/main.rs:14:30: 14:38 note: the type `[usize]` cannot be indexed by `&usize`
src/main.rs:14 &list.swap( &list[&j], &list[&j+1] );
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
note: in expansion of for loop expansion
src/main.rs:12:9: 16:10 note: expansion site
note: in expansion of for loop expansion
src/main.rs:11:5: 17:6 note: expansion site
src/main.rs:14:40: 14:51 error: mismatched types:
expected `usize`,
found `&usize`
(expected usize,
found &-ptr) [E0308]
src/main.rs:14 &list.swap( &list[&j], &list[&j+1] );
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
note: in expansion of for loop expansion
src/main.rs:12:9: 16:10 note: expansion site
note: in expansion of for loop expansion
src/main.rs:11:5: 17:6 note: expansion site
src/main.rs:14:40: 14:51 help: run `rustc --explain E0308` to see a detailed explanation
error: aborting due to 7 previous errors
Could not compile `bubble_sort`.
最佳答案
除了卢卡斯(Lukas)提到的问题之外,还有更多其他问题:
冒泡排序函数的参数必须具有&mut [usize]
类型。 list
的类型为[usize]
,因此您必须将其转换:在调用冒泡排序函数时。
bubble_sort(&mut list);
在列表上调用
swap
时,应传递要交换的内容的索引,而不是值本身。list.swap( j, j+1 );
关于rust - 错误: mismatched types: expected 'usize' found '&usize' raised while trying to implement bubble sort,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33321458/