【问题标题】:error: mismatched types: expected 'usize' found '&usize' raised while trying to implement bubble sort错误:不匹配的类型:在尝试实现冒泡排序时发现“&usize”预期的“usize”
【发布时间】:2016-01-24 02:50:44
【问题描述】:

我正在尝试在 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`.

【问题讨论】:

  • 请发布完整的编译器消息,这样我们就不必在您的代码中搜索错误。
  • 添加错误需要一点时间

标签: rust bubble-sort type-mismatch


【解决方案1】:

除了 Lukas 提到的之外,还有一些其他问题:


冒泡排序函数的参数类型必须为&amp;mut [usize]list[usize] 类型,所以你必须转换它:当你调用冒泡排序函数时。

bubble_sort(&mut list);

当您在列表中调用swap 时,您应该传入要交换的事物的索引,而不是值本身。

list.swap( j, j+1 );

【讨论】:

  • 注意list的类型不是[usize],而是[usize; 6],6代表其中的6个元素。这是一个重要的区别:[usize]unsized type[usize; N] 对于某些 N 完全不同。
【解决方案2】:

在不知道编译器错误的确切原因的情况下:您应该删除很多&amp;——例如在索引括号[ ]中。

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 2017-03-11
      • 1970-01-01
      • 2012-07-23
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      • 2014-02-20
      相关资源
      最近更新 更多