相关疑难解决方法(0)

为什么Rust需要明确的生命周期?

我正在阅读Rust书的生命周章,我在这个例子中看到了命名/显式生命周期:

struct Foo<'a> {
    x: &'a i32,
}

fn main() {
    let x;                    // -+ x goes into scope
                              //  |
    {                         //  |
        let y = &5;           // ---+ y goes into scope
        let f = Foo { x: y }; // ---+ f goes into scope
        x = &f.x;             //  | | error here
    }                         // ---+ f and y go out of scope
                              //  |
    println!("{}", x);        //  |
}                             // -+ x goes out …
Run Code Online (Sandbox Code Playgroud)

static-analysis reference lifetime rust

183
推荐指数
7
解决办法
2万
查看次数

在文档测试中使用本地模块时出错

我正在玩一个用于生成2D噪音的小箱子.这是我的"lib.rs"文件的简化片段:

pub mod my_math {
    pub struct Vec2<T> {
        ...
    }
    ...
}
pub mod my_noise {
    use num::Float;
    use std::num::Wrapping;
    use my_math::*;

    /// Gets pseudo-random noise based on a seed vector.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use my_math::Vec2;
    /// 
    /// let v_seed = Vec2::<f32>::new_values(4.134, -23.141);
    /// let noise_val = get_noise_white(&v_seed);
    /// 
    /// assert!(noise_val >= 0.0);
    /// assert!(noise_val <= 1.0);
    /// ```
    pub fn get_noise_white(seed: &Vec2<f32>) -> f32 {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我运行货物测试时,我收到以下错误:

---- my_noise …

testing documentation module rust

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