如何解决锈蚀测试不断出错我需要包括一些测试内容吗?
我的代码本身可以在文件中工作,但是每当我尝试运行Rustlings quiz1.rs时,代码的测试部分都会出错。
// GOAL OF PROGRAM
// Mary is buying apples. One apple usually costs 2 Rustbucks,but if you buy
// more than 40 at once,each apple only costs 1! Write a function that calculates
// the price of an order of apples given the order amount. No hints this time
fn calculate_apple_price(apples: i32){
let mut price = 2;
if apples >= 40 {
price = 1;
}
}
// Don't modify this function!
#[test]
fn verify_test() {
let price1 = calculate_apple_price(35);
let price2 = calculate_apple_price(65);
assert_eq!(70,price1); // Error happens here 'no implementation for `{integer} == ()`'
assert_eq!(65,price2);// Error happens here 'no implementation for `{integer} == ()`'
}
我在问题上进行了搜索,并尝试了rust的解释,但是我是Rust的新手。有人可以用简单的英语解释这个错误吗?
解决方法
您忘记为fn calculate_apple_price
指定返回类型,price1
和price2
的大小为零(这在您学习RUST时会造成混淆)。
你应该尝试这样的东西
fn calculate_apple_price(apples: i32) -> i32 /* specify i32 as return type */ {
let mut price = 2;
if apples >= 40 {
price = 1;
}
price // Return the price (the `return` keyword is optional in this case)
}
,
我把原来的功能弄错了。我要返回的是一个苹果的价格是多少。相反,我应该退还所有苹果的最终价格。这是该功能
import osmnx as ox
ox.config(use_cache=True,log_console=True)
G = ox.graph_from_place('Piedmont,CA,USA',network_type='drive')
Gp = ox.project_graph(G)
nodes = ox.graph_to_gdfs(Gp,edges=False)
c = nodes.unary_union.centroid
bbox = c.y + 500,c.y - 500,c.x - 500,c.x + 500
fig,ax = ox.plot_graph(Gp,bbox=bbox,figsize=(5,5))
,
显然接受的答案是错误的。
正确的一个:
fn calculate_apple_price(n:i32)-> i32 {
if n > 40 {
n
}else{
n * 2
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。