小编Tes*_*est的帖子

std::sync::Mutex 与 tokio::sync::Mutex 有什么区别?

与“普通”互斥体相比,什么是“异步”互斥体?Mutex我相信这是 tokio和普通 std lib之间的区别Mutex。但从概念上讲,我不明白互斥体如何“异步”。难道不是一次只有一件事可以使用它吗?

mutex rust rust-tokio

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

np.cumsum(df['column']) nan的处理

np.cumsum([1, 2, 3, np.nan, 4, 5, 6])将返回nan第一个之后的每个值np.nan。此外,它会对任何生成器执行相同的操作。然而,np.cumsum(df['column'])不会。做什么np.cumsum(...),以便对数据帧进行特殊处理?

In [2]: df = pd.DataFrame({'column': [1, 2, 3, np.nan, 4, 5, 6]})

In [3]: np.cumsum(df['column'])
Out[3]: 
0     1.0
1     3.0
2     6.0
3     NaN
4    10.0
5    15.0
6    21.0
Name: column, dtype: float64
Run Code Online (Sandbox Code Playgroud)

python numpy pandas

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

为什么 Rust 在必要时不会自动移动?

以下程序编译没有问题:

#[tokio::main]
async fn main() {
    async fn g(x: String) {}

    let f = || {
        let y: String = String::from("a").clone();

        return async {
        println!("{}", &y);
        return g(y).await;
    }};
}
Run Code Online (Sandbox Code Playgroud)

但是,如果行“return g(y).await;” 被删除后,它将失败并出现以下情况:

error[E0373]: async block may outlive the current function, but it borrows `y`, which is owned by the current function
  --> src/main.rs:35:22
   |
35 |           return async {
   |  ______________________^
36 | |         println!("{}", &y);
   | |                         - `y` is borrowed here
37 | |         // return g(y).await; …
Run Code Online (Sandbox Code Playgroud)

rust

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

定义 From<T> 后自动实现 From<&T>

考虑以下 MRE:

struct A {}
struct B {}

impl From<A> for B {
    fn from(t: A) -> B { B {} }
}
Run Code Online (Sandbox Code Playgroud)

是否可以自动实现From<&A> for B

rust

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

Python Polars 解析纪元日期

如何将一列 i64 纪元字符串转换为极坐标中的日期?

我有一列 i64 代表自纪元以来的秒数,我想将它们解析为极坐标本机日期时间。

python python-polars

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

无法在 Cargo.toml 中设置 rustflags target-cpu=native (SIMD-JSON)

我正在尝试编译 Rust simd-json 包。它抱怨该盒子不兼容 SIMD:

    |
221 | fn please_compile_with_a_simd_compatible_cpu_setting_read_the_simdjsonrs_readme() -> ! {}
    |    ----------------------------------------------------------------------------      ^ expected `!`, found `()`
    |    |
    |    implicitly returns `()` as its body has no tail or `return` expression
Run Code Online (Sandbox Code Playgroud)

然而,盒子是x86按照要求的:

    |
221 | fn please_compile_with_a_simd_compatible_cpu_setting_read_the_simdjsonrs_readme() -> ! {}
    |    ----------------------------------------------------------------------------      ^ expected `!`, found `()`
    |    |
    |    implicitly returns `()` as its body has no tail or `return` expression
Run Code Online (Sandbox Code Playgroud)

Cargo.toml包含所需的编译器标志:

$ uname -a
Linux ip-172-31-68-220 5.15.0-1011-aws #14-Ubuntu SMP Wed …
Run Code Online (Sandbox Code Playgroud)

simd rust rust-cargo

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

如何在 Rust 中获取字符串的最后 n 个字符?

在 python 中,你可以像这样获取字符串的最后 5 个字符:

s[-5:]
Run Code Online (Sandbox Code Playgroud)

如何在 Rust 中简洁地完成同样的事情?我能想到的最好的办法是非常冗长:

s.chars().rev().take(5).collect::<Vec<_>>().into_iter().rev().collect()
Run Code Online (Sandbox Code Playgroud)

rust

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

Rust:实现 Hash/Eq

在结构上实现的最简单方法是什么Hash/Eq,以便具有相同属性的两个不同实例不相等?

考虑以下结构:

struct Person {
    name: String,
}
Run Code Online (Sandbox Code Playgroud)

Hash/Eq即使两个不同的人具有相同的名字,最简单的实现方法是什么?是某件事的唯一途径Box<>吗?

rust

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

为什么不能返回对临时对象的引用?

为什么不能返回 Rust 中临时对象的引用?激励性的例子是:

fn what<'cellref, 'cell: 'cellref>(x: &f64) -> &'cellref CellValue<'cell> {
    &CellValue::Number(*x)
}
Run Code Online (Sandbox Code Playgroud)

人们可能认为编译器可以推断只要引用存在,该对象就需要存在,因此使其保持活动状态,直到引用被删除。为什么编译器不能推断出这一点?

根据要求,这是 MRE。它说明了一个简单的问题:假设 API 需要一个&T. 您将其存放在哪里,T以便其可以按照需要的时间持续使用&T

pub enum CellValue<'a> {
    String(&'a String),
    Number(f64),
    Boolean(bool),
    // ... many more types ...
}

pub enum Array<'a> {
    ArrayGeneric(ndarray::Array2<CellValue<'a>>),
    ArrayF64(ndarray::Array2<f64>),
    ArrayString(ndarray::Array2<f64>),
    // .. many more types ...
}

impl Array<'a> {
    pub fn generic_iter() -> Iterator<Item = &CellValue> {
        // ?
    }
}
Run Code Online (Sandbox Code Playgroud)

与许多 Rust 用例一样,目标是零成本的抽象。

作为动机,假设您有一个 Excel 电子表格,并且您正在定义一个可以在 Excel 函数之间传递的单元格“数组”。有时,这实际上是电子表格中的值范围。在其他时候,您可能已经应用了转换,例如ABS(...),现在您拥有了一系列单元格的绝对值的数组。在前一种情况下,您只需要存储完整的电子表格和一些 …

rust

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

Arc是如何实现的?怎么只有8个字节大?

我注意到 Rust 中的大小Arc只有 8 个字节。基本实现是什么Arc?我的感觉是,它至少需要一个指针和计数器,并且仅指针就已经有 8 个字节了。是一样的吗Rc

memory rust

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

标签 统计

rust ×8

python ×2

memory ×1

mutex ×1

numpy ×1

pandas ×1

python-polars ×1

rust-cargo ×1

rust-tokio ×1

simd ×1