我想从请求中获取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) 我有一个结构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) 在此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<'_>?
考虑以下 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 是后期绑定的,为什么它不应该在第二个程序中也后期绑定?请注意,我在第一个和第二个程序之间所做的唯一更改是添加另一个生命周期参数和一个依赖于这个新生命周期参数的返回类型。
我在项目中使用跟踪库,但有一件事我无法弄清楚:如何访问我的 中的值(我在创建它时在跨度中设置的值)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) 我尝试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) 我有一个程序属性宏,给定一个函数对每个二进制表达式进行操作,例如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) 我刚刚完成了关于 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) 我的应用程序使用tracingRust 箱来创建日志输出。当我运行测试时如何打印这些日志消息?
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)]呢?
rust ×10
rust-tracing ×2
actix-web ×1
arrays ×1
c ×1
generics ×1
lifetime ×1
logging ×1
mapping ×1
postgresql ×1
rust-cargo ×1
rust-diesel ×1
rust-macros ×1
syntax ×1
testing ×1
traits ×1
vector ×1