小编kmd*_*eko的帖子

Actix 2.0中如何从Request中获取Cookie

我想从请求中获取cookie的值。我发现在 Actix 0.xx 中,cookie 的值可以通过调用获取

fn get_cookie(req: HttpRequest) {
    let cookie = req.cookie("name") <-- Here

    return HttpResponse::Ok()
        .body(
            format!("{}", cookie);
        )
}
Run Code Online (Sandbox Code Playgroud)

我对 Rust 和 Actix 还很陌生。目前我正在从声明的函数解析它,该函数获取HttpRequest.headers(). 我不确定是否有像 Actix 0.xx 中那样直接获取 cookie 的方法

pub fn get_cookie(req: HttpRequest, name: &str) -> String {
    let cookie: Vec<&str> = req
        .headers()
        .get("cookie")
        .unwrap()
        .to_str()
        .unwrap()
        .split("&")
        .collect();

    let auth_token: Vec<&str> = cookie
        .into_iter()
        .filter(|each| {
            let body: Vec<&str> = each.split("=").collect();

            body[0] == name
        })
        .collect();

    let cookie_part: Vec<&str> = auth_token[0].split("=").collect();

    cookie_part[1].to_owned() …
Run Code Online (Sandbox Code Playgroud)

rust actix-web

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

如果我已经为 U 实现了 From&lt;T&gt;,Rust 是否会为 Vec&lt;U&gt; 实现 From&lt;Vec&lt;T&gt;&gt; ?

我有一个结构NotificationOption和另一个结构NotificationOption2以及From<NotificationOption>for的实现NotificationOption2

我想转换Vec<NotificationOption>Vec<NotificationOption2>

struct NotificationOption {
    key: String,
}

struct NotificationOption2 {
    key: String,
}

impl From<NotificationOption> for NotificationOption2 {
    fn from(n: NotificationOption) -> Self {
        Self {
            key: n.key,
        }
    }
}

let options: Vec<NotificationOption> = ...;
let options2: Vec<NotificationOption2> = options.into();
Run Code Online (Sandbox Code Playgroud)

但我收到编译器错误:

struct NotificationOption {
    key: String,
}

struct NotificationOption2 {
    key: String,
}

impl From<NotificationOption> for NotificationOption2 {
    fn from(n: NotificationOption) -> Self {
        Self { …
Run Code Online (Sandbox Code Playgroud)

generics vector traits rust parametric-polymorphism

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

Rust 中的 &lt;'_&gt; 是什么意思?

在此Debug示例代码中:

use std::fmt;

struct Point {
    x: i32,
    y: i32,
}

impl fmt::Debug for Point {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Point")
         .field("x", &self.x)
         .field("y", &self.y)
         .finish()
    }
}

let origin = Point { x: 0, y: 0 };

assert_eq!(format!("The origin is: {origin:?}"), "The origin is: Point { x: 0, y: 0 }");
Run Code Online (Sandbox Code Playgroud)

是什么<'_>意思f: &mut fmt::Formatter<'_>

syntax rust

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

为什么我可以在一种情况下指定延迟绑定生命周期参数,而在另一种情况下不能?

考虑以下 Rust 程序:

fn f<'a>(x: &'a i32) {
    unimplemented!();
}

fn main() {
    f::<'static>;
}
Run Code Online (Sandbox Code Playgroud)

编译时,输出如下编译错误:

fn f<'a>(x: &'a i32) {
    unimplemented!();
}

fn main() {
    f::<'static>;
}
Run Code Online (Sandbox Code Playgroud)

让我们像这样修改程序:

fn f<'a, 'b>(x: &'a i32) -> &'b i32 {
    unimplemented!();
}

fn main() {
    f::<'static>;
}
Run Code Online (Sandbox Code Playgroud)

由于某些奇怪的原因,现在编译时没有任何编译错误。为什么是这样?如果第一个程序中的生命周期参数 'a 是后期绑定的,为什么它不应该在第二个程序中也后期绑定?请注意,我在第一个和第二个程序之间所做的唯一更改是添加另一个生命周期参数和一个依赖于这个新生命周期参数的返回类型。

lifetime rust

6
推荐指数
0
解决办法
156
查看次数

如何访问跟踪范围中字段的值?

我在项目中使用跟踪库,但有一件事我无法弄清楚:如何访问我的 中的值(我在创建它时在跨度中设置的值)Layer

我的图层看起来像这样:

impl<S> Layer<S> for CustomLayer where S: Subscriber {
    fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest {
        Interest::sometimes() //hardcoding so enabled() will be called everytime a span is created
    }

    fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool {
        if metadata.is_span() {
            // How do I access value of key here?
            if value == X {
                true
            } else if value == Y {
                false
            }
        }
        true // default
    }
}
Run Code Online (Sandbox Code Playgroud)

rust rust-tracing

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

如何安装diesel_cli并出现“无法打开输入文件libpq.lib”错误?

我尝试diesel_cli在我的机器上安装并遇到错误:

PS C:\> cargo install diesel_cli --no-default-features --features postgres
Run Code Online (Sandbox Code Playgroud)
error: linking with `link.exe` failed: exit code: 1181
  |
  = note: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Tools\\MSVC\\14.29.30037\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "C:\\Users\\Owner\\AppData\\Local\\Temp\\rustc0hfMW4\\symbols.o" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\diesel-e71e8d3863826309.diesel.db6871c2-cgu.0.rcgu.o" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\diesel-e71e8d3863826309.diesel.db6871c2-cgu.1.rcgu.o" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\diesel-e71e8d3863826309.diesel.db6871c2-cgu.10.rcgu.o" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\diesel-e71e8d3863826309.diesel.db6871c2-cgu.11.rcgu.o" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\diesel-e71e8d3863826309.diesel.db6871c2-cgu.12.rcgu.o" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\diesel-e71e8d3863826309.diesel.db6871c2-cgu.13.rcgu.o" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\diesel-e71e8d3863826309.diesel.db6871c2-cgu.14.rcgu.o" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\diesel-e71e8d3863826309.diesel.db6871c2-cgu.15.rcgu.o" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\diesel-e71e8d3863826309.diesel.db6871c2-cgu.2.rcgu.o" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\diesel-e71e8d3863826309.diesel.db6871c2-cgu.3.rcgu.o" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\diesel-e71e8d3863826309.diesel.db6871c2-cgu.4.rcgu.o" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\diesel-e71e8d3863826309.diesel.db6871c2-cgu.5.rcgu.o" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\diesel-e71e8d3863826309.diesel.db6871c2-cgu.6.rcgu.o" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\diesel-e71e8d3863826309.diesel.db6871c2-cgu.7.rcgu.o" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\diesel-e71e8d3863826309.diesel.db6871c2-cgu.8.rcgu.o" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\diesel-e71e8d3863826309.diesel.db6871c2-cgu.9.rcgu.o" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\diesel-e71e8d3863826309.3oc4g8xgeqr3xhil.rcgu.o" "/LIBPATH:C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps" "/LIBPATH:C:\\Users\\Owner\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libdiffy-10116d1a46240765.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libansi_term-8ac785ba6dab3d54.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\liburl-085413cf155ebf4e.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libidna-e617760a188ae951.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libunicode_normalization-06cbce6ee463ab15.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libtinyvec-2700dcc8ca440a73.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libtinyvec_macros-5d0a0a5267ac9a02.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libunicode_bidi-3843cd708b9d5313.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libform_urlencoded-bc0ece478c9969ff.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libpercent_encoding-459d5072c4f6d412.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libdotenvy-d2dbc9637619d426.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libchrono-422b701f158856a2.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libnum_integer-2ae42b6e08c222a4.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libnum_traits-c406e65b13d6dcc2.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libserde_regex-766adf3a555ccd38.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libregex-f7212d89a6f67148.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libaho_corasick-5aae31e8a72badf2.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libmemchr-a65d51fb2ae7accd.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libregex_syntax-5a1f463340fb2444.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libheck-f97b054e733bf2b0.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libclap_complete-007464ddcbb6d7e3.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libdiesel_migrations-3bdc5b636ff49b93.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libmigrations_internals-057e468a6688077a.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libtoml-761527a98d007259.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libdiesel-3216c6f0692e5464.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libitoa-275f0509159cbce5.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libbyteorder-c046fcd7b7138a94.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libpq_sys-634b13f8c11ea75f.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libclap-5c0a23e17fdd1cb3.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libatty-8c20fbe3a5f86f4a.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libstrsim-3f67262decc03c33.rlib" "C:\\Users\\Owner\\AppData\\Local\\Temp\\cargo-installUiMeci\\release\\deps\\libtermcolor-2d2dc574ed6f0782.rlib" …
Run Code Online (Sandbox Code Playgroud)

postgresql rust rust-cargo rust-diesel

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

有没有办法获取宏中变量的类型?

我有一个程序属性宏,给定一个函数对每个二进制表达式进行操作,例如let a = b + c;并根据它返回另一个表达式。根据类型进行操作,需要知道、、+的类型。abc

有没有办法在编译时获取变量的推断类型?

(就像 rust-analysisr 可能会显示推断的类型一样,我可以在宏中获取这些类型吗?)

简单示例 -游乐场

为了在 Rust 游乐场中更简洁地说明我的方法,我们可以使用声明性宏来调用给定变量上的函数,该函数的细节基于给定变量的类型。

我在 Rust 游乐场中最接近我想要的功能:

macro_rules! SomeMacro {
    ($x:expr) => {{
        $x.some_function(3.)
    }};
}
trait SomeTrait {
    fn some_function(&self,x:f32) -> f32;
}
impl SomeTrait for u32 {
    fn some_function(&self,x:f32) -> f32 {
        x * 3.
    }
}

fn main() {
    let a = 3u32;
    let b = SomeMacro!(a);
    assert_eq!(b,9.);
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能让事情像这样工作:

fn some_function<u32>(x:f32) -> f32 {
    3. * x …
Run Code Online (Sandbox Code Playgroud)

rust rust-macros

6
推荐指数
2
解决办法
3096
查看次数

有没有办法在 Rust 中获取 C99 数组指示符或替代方案?

我刚刚完成了关于 C 和 Rust元音计数的 CodeWars kata。示例代码简单明了。数组可以用作快速映射。在这里,我将字符映射到逻辑值(0 或 1)。

C实现:

#include <stddef.h>
#include <stdint.h>

// :) This const array design looks smart & IMO-readable. Use of C99 feature.
const uint8_t areVowels[256]= {['a']=1, ['e']=1, ['i']=1, ['o']=1,  ['u']=1};

size_t get_count(const unsigned char *s)
{
  auto size_t count= 0;
  for (;*s!='\0';s++){
    count+= areVowels[*s];
  }
  return count;
}
Run Code Online (Sandbox Code Playgroud)

Rust 实现:

// :( Here is me pain. Unreadable, python3-generated array content.
const ARE_VOWELS:[u8;256]= [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];

fn get_count(s: &str) -> usize {
  let mut vowels_count: usize …
Run Code Online (Sandbox Code Playgroud)

c arrays mapping initialization rust

6
推荐指数
3
解决办法
210
查看次数

如何在测试中打印跟踪输出?

我的应用程序使用tracingRust 箱来创建日志输出。当我运行测试时如何打印这些日志消息?

testing logging rust rust-tracing

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

为什么用`#[cfg(test)]`注释的无效代码仍然会导致构建失败?

cargo build以下代码运行将成功:

#[cfg(test)]
mod tests {
    #[test]
    fn func() { 
        let x = 1;
        sss
    }
}
Run Code Online (Sandbox Code Playgroud)

但此代码会失败:

#[cfg(test)]
mod tests {
    #[test]
    fn func() {
        sss
        let x = 1;
    }
}
Run Code Online (Sandbox Code Playgroud)
#[cfg(test)]
mod tests {
    #[test]
    fn func() { 
        let x = 1;
        sss
    }
}
Run Code Online (Sandbox Code Playgroud)

《Rust Book》中关于测试组织的一节说:

测试模块上的注释#[cfg(test)]告诉 Rust 仅在运行时编译并运行测试代码cargo test,而不是在运行时编译 和运行测试代码cargo build

那么为什么 Rust 仍然可以编译mod tests带有 注释的代码#[cfg(test)]呢?

conditional-compilation rust

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