繁体   English   中英

错误:类型不匹配:尝试实现冒泡排序时,出现了预期的'usize',并出现了'&usize'

[英]error: mismatched types: expected 'usize' found '&usize' raised while trying to implement bubble sort

我试图在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 );

不知道导致编译器错误的确切原因:例如,应该删除索引括号[ ]中的& -。

编译器只是说它期望一个类型为usize的变量,但是找到了&usize类型的一个引用该类型的变量。 索引运算符( [ ]括号)采用类型为usize的参数。 但是您可以通过添加&提供参考。

谢谢大家,下面是正确的实现:

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 );
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM