- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 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/
CREATE OR REPLACE FUNCTION mover(src text, dst text, cpquery text, conname text, ifbin boolean) retu
在python中,raise和raise e在except block 中有区别吗? dis 向我显示不同的结果,但我不知道这是什么意思。 两者的最终行为是什么? import dis def a()
当使用 pytest.raises 测试由 try/except block 捕获的错误时,由于未引发错误而失败。 如果我使用字典查找进行简单测试,并且不对其进行 try/except 操作,则 py
我有以下代码 if self.download_format == 'mp3': raise NotImplementedError elif self.downloa
在技术、哲学、概念或其他方面有什么区别 raise "foo" 和 raise Exception.new("foo") ? 最佳答案 从技术上讲,第一个引发 RuntimeError,消息设置为 "
定义无参数异常: class MyException(Exception): pass 当提出时,有什么区别: raise MyException 和 raise MyException()
标题很容易理解——它们之间的区别是什么 raise Exception, "foo" 和 raise Exception("foo") 它做的事情是否完全相同,只是语法不同? 我使用的是 Python
为了处理 Rails 异常,我看到人们使用“raise SomeException.new”或“raise SomeException”,有什么区别? 说如果我有课 class UnableToCr
当我从终端运行我的 .py 文件时,我没有收到任何错误,但是当我作为可执行文件(完全相同的应用程序)运行时,我在这一行收到错误 raise RuntimeError(f'Line lengths in
我在编写一小段代码时遇到了困难: act_therm.sa_handler=handler_therm; sigaction(SIGUSR1,&act_thermom,NULL); w
我试图在此页面中显示四张照片: http://progenygenealogy.com/products/family-tree-charts/photo-gallery-2.aspx 我怎样才能让左
什么是raise A, B做?它与raise A有何不同?是吗? 一些例子(运行在python 2.7的解释器上): class E(Exception): pass e = E() rais
如何优雅地实现 "Samurai principle" (返回胜利,或者根本没有)我的功能? return if else raise 最佳答案 如果你绝对想在表达式中 raise,你可以这样做
Python中的raise和raise from有什么区别? try: raise ValueError except Exception as e: raise IndexError
Python中try块可以捕获测试代码块中的错误。except块可以处理错误。finally块可以执行代码,而不管try-和except块的结果如何。本文主要介绍Python 抛出引发异常(rais
我正在用 d3js 构建一个网络(基本上是节点和链接) 当我将鼠标悬停在一个节点上时,我想突出显示关联的链接并将它们置于父节点的顶部以使其真正可见 在鼠标悬停时,我做了这样的东西 // get lin
在 WPF 中,我正在尝试使用 moq 来引发一个事件,该事件具有与其 Hook 的异步监听器: 我的代码: public class Provider { private I
我在这段代码(来自 https://www.django-rest-framework.org/tutorial/3-class-based-views/ )上有一条 pylint 消息(w0707)
我有一个带有 Let 函数的类模块,该函数会引发自定义错误,示例如下所示 Private pValue As Double Public Property Let Value(v As Double)
我试图将表限制为只有一条记录,并禁止所有添加更多记录的尝试。我创建了此触发器:CREATE TRIGGER abort_insert_to_my_tbl BEFORE INSERT ON my_tbl
我是一名优秀的程序员,十分优秀!