小编Asn*_*ari的帖子

理解 Rust 中的filter_map

我正在努力找出ages所有Person有第二个名字的人。我能够使用filtermap分别进行如下操作,但我想知道我们是否可以filter_map在 Rust 中使用。

但我无法弄清楚。

struct Person {
    pub first_name: String,
    pub last_name: Option<String>,
    pub age: i32,
}


fn main() {
    let mut persons: Vec<Person> = Vec::new();
    persons.push(Person {
        first_name: "Asnim".to_string(),
        last_name: None,
        age: 1,
    });
    persons.push(Person {
        first_name: "Fahim".to_string(),
        last_name: Some("Ansari".to_string()),
        age: 2,
    });
    persons.push(Person {
        first_name: "Shahul".to_string(),
        last_name: None,
        age: 6,
    });
    persons.push(Person {
        first_name: "Mujeeb".to_string(),
        last_name: Some("Rahuman".to_string()),
        age: 6,
    });

    let ages_of_people_with_second_name_using_seperate_filter_map: Vec<i32> = persons
        .iter()
        .filter(|p| p.last_name.is_some()) …
Run Code Online (Sandbox Code Playgroud)

rust

20
推荐指数
2
解决办法
4万
查看次数

如何在 sqlx rust 中定义日期时间字段

我有一个结构如下?

pub struct Instrument {
  pub id:i32,
  pub expiry_on: <this should be a date field>
}
Run Code Online (Sandbox Code Playgroud)

我应该给出什么类型expiry_on。我想使用里面的结构sqlx::query_as!()来获取记录postgres

expiry_on是一个timestampz Postgres字段。

rust

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

如何阻止pycharm在虚拟环境文件夹中搜索?

.venv在项目根目录中有一个文件夹,其中有一个 python 虚拟环境。当我使用 Pycharm 的搜索功能时,它还会显示来自该.venv 文件夹的结果。如何从搜索结果中忽略虚拟环境文件夹?

我尝试将目录标记为Excluded,但这没有帮助。

pycharm

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

如何将枚举的 Vec 定义为 rust sqlx 模型中的字段

我正在尝试将 Postgres 中的数组字段加载到 Rust 结构中,如下所示

use sqlx::{Pool, PgConnection, PgPool, FromRow};
use sqlx::postgres::PgQueryAs;

#[derive(Copy, Clone, sqlx::Type)]
#[sqlx(rename = "VARCHAR")]
#[sqlx(rename_all = "snake_case")]
enum Coupon {
    ChristmasSaleCoupon,
    NewYearSaleCoupon,
}

#[derive(FromRow, Clone)]
struct UserCouponMap {
    pub id: i64,
    pub user_id: i64,
    pub coupons: Vec<Coupon>,
}

impl UserCouponMap {
    pub async fn get_for_userid(db_pool: Pool<PgConnection>, user_id: i64) -> Vec<UserCouponMap> {
        let user_coupon_map: Vec<UserCouponMap> = sqlx::query_as("SELECT * FROM user_coupon_map WHERE user_id = $1")
            .bind(user_id)
            .fetch_all(db_pool)
            .await
            .expect("failed to fetch user coupon map");
        user_coupon_map
    }
}


#[tokio::main] …
Run Code Online (Sandbox Code Playgroud)

rust rust-sqlx

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

Python套接字等待

我想知道是否有一种方法可以告诉 python 等待它从服务器获得响应以继续运行。

我正在写一个回合制游戏。我进行了第一步,它将移动发送到服务器,然后将服务器发送到另一台计算机。问题来了。由于不再轮到我,我希望我的游戏等到它得到服务器的响应(等到其他玩家采取行动)。但我的线路:

data=self.sock.recv(1024)
Run Code Online (Sandbox Code Playgroud)

挂起是因为(我认为)它没有立即得到一些东西。所以我想知道如何让它等待某些事情发生然后继续前进。

提前致谢。

python sockets pygame

5
推荐指数
1
解决办法
2万
查看次数

是否可以在编辑器中而不是在Web浏览器中运行React.js调试器

我想通过在WebStorm中而不是在Web浏览器中添加断点来调试React.js项目。

可能吗?如果是,怎么办?

node.js webstorm reactjs

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

如何在 sqlx rust 中运行嵌套查询?

我想在 sqlx 中运行嵌套查询。这是我尝试过的:

sqlx::query_as!(NseData, "select * from nse_data  where trading_day = (select max(trading_day) from nse_data)").fetch_one(&app_context.db_connection).await?;
Run Code Online (Sandbox Code Playgroud)

但它给了我以下错误......

error[E0658]: attributes on expressions are experimental
  --> db/src/nse_data.rs:30:9
   |
30 |         sqlx::query_as!(NseData, "select * from nse_data  where trading_day = (select max(trading_day) from nse_data)").await?
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
Run Code Online (Sandbox Code Playgroud)

rust rust-sqlx

5
推荐指数
0
解决办法
869
查看次数

如何将 docker cron 日志写入 stdout/stderr

我有使用CMD ["cron","-f"]命令运行 cron 的 docker 。这会将输出写入 cron 日志文件。

有什么办法可以将这些日志重定向到控制台。

我的基本图像看起来像

FROM ubuntu:latest
RUN ls

RUN apt-get update
RUN apt install -y software-properties-common
RUN add-apt-repository -y ppa:openjdk-r/ppa
RUN apt-get update
RUN apt-get install -y openjdk-12-jre

RUN apt-get -y install cron wget unzip



RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list \
    && apt-get update -qqy \
    && apt-get -qqy install google-chrome-stable \
    && rm /etc/apt/sources.list.d/google-chrome.list \
    && rm …
Run Code Online (Sandbox Code Playgroud)

cron stdout docker

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

我们如何在 sqlx rust 中定义 jsonb 和 UUID 字段?

我有一个 Postgres 表,其中包含三个字段 id,即 a bigserialmetaajsonb字段和 uuidUUID字段。

pub struct MetaLogs {

    pub id:i64,
    pub uuid: <what type should I give here > 
    pub meta: < What type should I give here > 
}
Run Code Online (Sandbox Code Playgroud)

我正在使用sqlxORM 进行Rust. 虽然我明白我必须添加

features = [ "runtime-tokio", "macros" ,"postgres","json","uuid"]
Run Code Online (Sandbox Code Playgroud)

我不知道之后如何继续

rust rust-sqlx

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

有没有办法检测Python中的循环是否停止

我试图检测我的循环何时停止,以便我可以使用 if 语句说:如果“循环已停止”:print(text)

代码:

faces = detector(gray)

for face in faces:

    x, y = face.left(), face.top()
    x1, y1 = face.right(), face.bottom()
    rectangle = cv2.rectangle(frame, (x, y), (x1, y1), (0, 255, 0), 2)

    if "the loop has stopped":
        cv2.putText(frame, "Asleep", (50, 150), font, 7, (0, 0, 255))
Run Code Online (Sandbox Code Playgroud)

python for-loop if-statement

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