gpt4 book ai didi

rust - 错误 : mismatched types: expected 'usize' found '&usize' raised while trying to implement bubble sort

转载 作者:行者123 更新时间:2023-11-29 08:10:13 32 4
gpt4 key购买 nike

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

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com