相关疑难解决方法(0)

为什么删除返回给我一个错误:期望类型`()`但找到类型

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)

替换truefalse使用return true;/ return false;工作,但为什么使用以前不编译?

rust

8
推荐指数
1
解决办法
2091
查看次数

预期枚举 `std::result::Result`,在使用 for 循环时找到 `()`

我正在修改纹理合成项目中的一个示例:

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

1
推荐指数
1
解决办法
535
查看次数

标签 统计

rust ×2