我正在尝试在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`.
答案 0 :(得分:5)
冒泡排序功能的参数必须为&mut [usize]
类型。 list
的类型为[usize]
,因此您必须将其转换为:当您调用冒泡排序函数时。
bubble_sort(&mut list);
当您在列表上调用swap
时,您应该传递要交换的内容的索引,而不是值本身。
list.swap( j, j+1 );
答案 1 :(得分:3)
在不知道编译器错误的确切原因的情况下:例如,您应该在索引括号&
中删除相当多的[ ]
。
编译器只是说它期望一个类型为usize
的变量,但是找到了&usize
类型的变量,它是对该类型的引用。索引运算符([ ]
括号)采用类型usize
的参数。但是,您可以通过添加&
来提供参考。
答案 2 :(得分:1)
谢谢大家和以下是正确的实施:
fn main() {
let mut list = [15, 3, 2, 1, 6, 0];
bubble_sort(&mut list);
println!("Sorted list is: {:?}", &list);
}
fn bubble_sort(list: &mut [usize]) {
for _ in 0..list.len() {
for j in 0..(&list.len()-1) {
if list[j] > list[j+1] {
list.swap( j, j+1 );
}
}
}
}