- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码。它有效。
但我更感兴趣的是写make_tea
我在哪里调用两个函数:get_milk_from_cow
和 pour_milk
.他们都返回 Result<String, TeaError>
.
我如何组合它们才能继续连接 String
s 如果他们成功,否则返回错误。
enum TeaError {
NoMilk,
NoCup,
NoCow,
NoGas,
NoSomething,
}
fn get_milk_from_cow(cow: bool) -> Result<String, TeaError> {
if cow {
Ok(String::from("get milk from cow"))
} else {
Err(TeaError::NoCow)
}
}
fn pour_milk(milk: bool) -> Result<String, TeaError> {
if milk {
Ok(String::from("poured milk"))
} else {
Err(TeaError::NoMilk)
}
}
fn make_tea() -> Result<String, TeaError> {
let mut process = String::new();
let step_cow = get_milk_from_cow(true)?;
let step_milk = pour_milk(true)?;
process.push_str(step_cow.as_str());
process.push_str(step_milk.as_str());
Ok(process)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn attemp_tea_one() {
match pour_milk(false) {
Err(v) => match v {
TeaError::NoMilk => assert!(true),
_ => assert!(false)
},
Ok(_) => assert!(false)
};
}
#[test]
fn attemp_tea_two() {
match make_tea() {
Err(_) => assert!(false),
Ok(_) => assert!(true)
};
}
}
我试过了:
process.push_str(get_milk_from_cow(true)?
.push_str(pour_milk(true)?.as_str()))
但它给出了:
error[E0308]: mismatched types
--> src/errors.rs:27:22
|
27 | process.push_str(get_milk_from_cow(true)?.push_str(pour_milk(true)?.as_str()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &str, found ()
|
= note: expected type `&str`
found type `()`
作为push_str
不返回字符串。
编辑:
fn append(s1: String, s2: String) -> String {
s1 + s2.as_str()
}
fn make_tea() -> Result<String, TeaError> {
let process = append(get_milk_from_cow(true)?,
pour_milk(true)?);
Ok(process)
}
所以,我可以做到append(append(funcA(), funcB()), funcC())
等等..我还在学习关于生命周期的知识,并思考是否append
在内存分配方面仍有待改进。
最佳答案
此代码在带星号的行上做了多余的工作:
fn make_tea() -> Result<String, TeaError> {
* let mut process = String::new();
let step_cow = get_milk_from_cow(true)?;
let step_milk = pour_milk(true)?;
* process.push_str(step_cow.as_str());
process.push_str(step_milk.as_str());
Ok(process)
}
process.push_str(step_cow.as_str())
只是生成一个不需要的 step_cow
副本。相反,尝试
fn make_tea() -> Result<String, TeaError> {
let mut process = get_milk_from_cow(true)?;
process.push_str(&pour_milk(true)?);
Ok(process)
}
或者,更方便的是,
fn make_tea() -> Result<String, TeaError> {
Ok(get_milk_from_cow(true)? + &pour_milk(true)?)
}
关于error-handling - 在 Rust : concating results on Ok 中编写错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43776706/
我正在使用以下代码读取我在文本编辑器 (Notepad++) 中创建的文本 (.xml) 文件,将我从中读取的 UTF-8 文本转换为 UTF-16,以便 Windows API 函数可以使用它,然后
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 1年前关闭。 Improve this
我知道所有的论坛都充满了这样的问题,但我尝试了几个钩子(Hook),但它们不起作用(或者我做的不好)。 所以,我有: main.cpp <- fawn.h <- connector.cpp (defe
这是我正在使用的一段代码。 当 items 为 null 时,ok(Object items) 方法在内部调用 Jersey 的 Response.ok() 方法。 MembershipReq
我在 Tcl/Tk 中有一个简单的窗口,点击确定按钮运行模拟。我正在使用 Linux。模拟完成后,窗口将被销毁。问题是在模拟运行时窗口仍然存在。我希望窗口在我点击确定按钮后消失。 我尝试使用 wm w
在我们的网络应用程序中,我将 Angular-Materials $mdDialog 与确认对象一起使用。是否可以将按钮的顺序从取消-确定更改为确定-取消?并将初始焦点设置为取消按钮?也许通过 CSS
这个问题在这里已经有了答案: 关闭10年前。 Possible Duplicate: Ternary conditional operator in Python 我有这个问题,不知道要问谷歌: (v
我尝试使用 R 进行回归。我有以下代码,导入 CSV 文件没有问题 dat <- read.csv('http://pastebin.com/raw.php?i=EWsLjKNN',sep="
在 QInputDialog 中,如何去掉 OK 和 Cancel 按钮中的图标? 注意取消和确定的图标。我查看了属性按钮,不知道如何删除它们。 最佳答案 解决方案的策略是先获取按钮,但是这些属于QD
当使用Postman测试项目时,任何POST方法,我收到的是200 OK而不是201 Created,并且subreddit不是在数据库中创建的,并且在控制台休眠中接收到以下内容:SELECT T1_
当使用Postman测试项目时,任何POST方法,我收到的是200 OK而不是201 Created,并且subreddit不是在数据库中创建的,并且在控制台休眠中接收到以下内容:SELECT T1_
我制作了一个安卓应用程序,可以从本地 wifi 网络传输语音。为了收听和流式传输,我使用 JNI 中的 Opus C API 进行解码,并使用 OpenSL Audio 进行读取。 我从 Servic
我有一个定义如下的 map : mapMeasures := make(map[time.Time]models.Measure, 0) 与 type Measure struct { Del
这里我的数据集是 pd我已将其拆分为训练和测试数据 pd_train1和 pd_train2 sku national_inv lead_time in_transit_qty forecas
我已经检查过有关此问题的其他问题,但由于问题似乎非常具体,因此它们没有帮助。 我有一个像这样的数据框(这只是一个简单的示例,下面提供了来自 dput() 的示例数据): year species ab
当我使用 anova_test() 函数(来自 rstatix 包)做双向重复测量方差分析时,出现错误: lm.fit(x, y, offset = offset, singular.ok = sin
我一直在尝试对数据集进行 2-Way 重复测量测试,年份和疫苗类型是自变量,覆盖率是因变量。我用代码运行它: sat = anova_test( data=SA, dv = coverage, w
奇遇 我是一个普通的大学生,尹成是我的名字。顾名思义,我和其他人一样,没有什么特别之处。然而,在某个偶然的机会下,我发现了一个全新的领域——编程。 初印象 说实话,我对编程并不了解,甚至可以说是一窍不
我使用这些代码创建了一个卡拉 OK 并将其刻录到 VCD。 xxxxxx_1.m4a 文件是左声道(乐器),xxxxxx_0.m4a 文件是右声道(带人声的歌曲)。 将它们组合到 become xxx
我不确定为什么会发生这种情况,但我有一个简单的 Ajax 代码: $.ajax({ url: "/javascript/testing.js"}) .done(function(data){
我是一名优秀的程序员,十分优秀!