我is_prime在Rust中编写了一个函数,我的印象是简单的写作true相当于return true;,但在我的函数中并非如此:
fn is_prime(number: i64) -> bool {
for i in 2i64..number {
if number % i == 0 && i != number {
false
}
}
true
}
Run Code Online (Sandbox Code Playgroud)
这会给我错误:
error[E0308]: mismatched types
--> src/lib.rs:4:13
|
4 | false
| ^^^^^ expected (), found bool
|
= note: expected type `()`
found type `bool`
Run Code Online (Sandbox Code Playgroud)
替换true和false使用return true;/ return false;工作,但为什么使用以前不编译?
我正在修改纹理合成项目中的一个示例:
use texture_synthesis as ts;
fn main() -> Result<(), ts::Error> {
//create a new session
let texsynth = ts::Session::builder()
//load a single example image
.add_example(&"imgs/1.jpg")
.build()?;
//generate an image
let generated = texsynth.run(None);
//save the image to the disk
generated.save("out/01.jpg")
}
Run Code Online (Sandbox Code Playgroud)
我想用for循环重复这三遍。这就是我认为我可能会这样做的方式:
use texture_synthesis as ts;
fn main() -> Result<(), ts::Error> {
for i in 0..3 {
let texsynth = ts::Session::builder()
//load a single example image
.add_example(&"imgs/1.jpg")
.build()?;
//generate an image
let generated = texsynth.run(None);
//save …Run Code Online (Sandbox Code Playgroud) rust ×2