我正在构建一个从 Raku NativeCall 到 Rust Polars 的接口,以获得很酷的 Arrow2 性能提升。在高层,我想使用 Polars 结构(例如 DataFrame 和 Series)作为匹配容器的属性。所以df.column我想要这样的东西......
use polars::prelude::*;//{CsvReader, DataType, Field, Schema, DataFrame,};
use polars::prelude::{Result as PolarResult};
use polars::frame::DataFrame;
use polars::datatypes::DataType;
pub struct DataFrameC {
df: DataFrame,
}
impl DataFrameC {
fn new() -> DataFrameC {
DataFrameC {
df: DataFrame::default(),
}
}
fn column(&self, string: String) -> Series {
//let colin = self.df.column(&string).unwrap().head(Some(23));
let colin = self.df.column(&string).unwrap()
println!{"{}", colin};
colin
}
}
Run Code Online (Sandbox Code Playgroud)
(系列的类似方法 - 因此完成此 fn 的下一步是创建 aSeries::new()然后se.set(colin) …
从我自己的理解和实验来看,这似乎是正确的,但我还没有找到记录它的权威来源。Rust by Example 有一个边界部分,其中写道:
T: 'a: 中的所有引用都T必须比生命周期长'a。Run Code Online (Sandbox Code Playgroud)#[derive(Debug)] struct Ref<'a, T: 'a>(&'a T); // `Ref` contains a reference to a generic type `T` that has // an unknown lifetime `'a`. `T` is bounded such that any // *references* in `T` must outlive `'a`. Additionally, the lifetime // of `Ref` may not exceed `'a`.
然而,这看起来是一个很糟糕的演示,因为T: 'a界限似乎并不影响 的行为Ref。我尝试构造一个T比 短的 ,'a无论有没有 都会受到阻碍T: 'a。更重要的是,一个没有生命周期限制的通用引用可以被当作一个有生命周期的引用 …
这类似于How do I use a custom comparator function with BTreeSet? 但就我而言,直到运行时我才会知道排序标准。可能的标准很广泛,并且不能进行硬编码(想想像按到目标的距离排序或按有效负载中的特定字节或其组合排序)。创建地图/集合后,排序标准不会更改。
我看到的唯一替代方案是:
Vec,但 log(n) 插入和删除至关重要这对于标准 C++ 容器std::map/是可能的,但对于 Rust 的/std::set似乎不可能。标准库或其他板条箱中是否有替代方案可以做到这一点?或者我必须自己实施这个?BTreeMapBTreeSet
我的用例是一个类似数据库的系统,其中集合中的元素由模式定义,例如:
Element {
FIELD x: f32
FIELD y: f32
FIELD z: i64
ORDERBY z
}
Run Code Online (Sandbox Code Playgroud)
但由于模式是用户在运行时定义的,因此元素存储在一组字节 ( BTreeSet<Vec<u8>>) 中。同样,元素的顺序是用户定义的。所以我会给的比较器BTreeSet看起来像|a, b| schema.cmp(a, b)。硬编码后,上面的示例可能类似于:
fn cmp(a: &Vec<u8>, b: &Vec<u8>) -> Ordering {
let a_field = self.get_field(a, 2).as_i64();
let b_field = self.get_field(b, 2).as_i64(); …Run Code Online (Sandbox Code Playgroud) 我有一个 Base64 图像,并Vec<u8>从中获取并将它们写入文件。这是我的代码:
let file_contents_base64: Vec<u8> = image_base64::from_base64(
String::from(&file_contents_string_vec[0])
);
Run Code Online (Sandbox Code Playgroud)
我想将file_contents_base64变量写入文件。
我想轮询一个异步函数:
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
some_function().await;
}
Run Code Online (Sandbox Code Playgroud)
我目前正在激活所有功能:
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
some_function().await;
}
Run Code Online (Sandbox Code Playgroud)
其中哪些是必要的?
tokio = { version = "1.4.0", features = ["full"] }
Run Code Online (Sandbox Code Playgroud) 我已从使用actix-web3.xx 转向 4.xx,之前运行良好的代码现在抛出此错误:
the trait bound `fn(actix_web::web::Query<TweetParams>, actix_web::web::Data<Pool<Postgres>>) -> impl std::future::Future {tweets4}: Handler<_, _>` is not satisfied
--> src/routes/all_routes.rs:74:14
|
74 | pub async fn tweets4(
| ^^^^^^^ the trait `Handler<_, _>` is not implemented for `fn(actix_web::web::Query<TweetParams>, actix_web::web::Data<Pool<Postgres>>) -> impl std::future::Future {tweets4}`
Run Code Online (Sandbox Code Playgroud)
经过一番谷歌搜索后,似乎生态系统中确实存在一个Handler特征actix(但是,我认为不是 actix-web)。
我不知道我需要在哪里实现该特征。错误消息似乎表明函数本身缺少它,但我的理解是你只能在structs和上实现特征enums,而不是函数?
这是处理程序代码:
#[get("/tweets4")]
pub async fn tweets4(
form: web::Query<TweetParams>,
pool: web::Data<PgPool>,
) -> Result<HttpResponse, HttpResponse> {
let fake_json_data = r#"
{ "name": "hi" }
"#; …Run Code Online (Sandbox Code Playgroud) 当我尝试使用axum构建应用程序时,我未能将框架与处理程序分开。对于Go,经典的方法是定义一个Interface,实现它并将处理程序注册到框架。通过这种方式,可以很容易地提供一个模拟处理程序来进行测试。然而,我无法让它与 Axum 一起工作。我trait像上面一样定义了 a ,但它不会编译:
use std::net::ToSocketAddrs;
use std::sync::{Arc, Mutex};
use serde_derive::{Serialize, Deserialize};
use serde_json::json;
use axum::{Server, Router, Json};
use axum::extract::Extension;
use axum::routing::BoxRoute;
use axum::handler::get;
#[tokio::main]
async fn main() {
let app = new_router(
Foo{}
);
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}
trait Handler {
fn get(&self, get: GetRequest) -> Result<GetResponse, String>;
}
struct Foo {}
impl Handler for Foo {
fn get(&self, req: GetRequest) -> Result<GetResponse, String> {
Ok(GetResponse{ message: "It works.".to_owned()})
}
} …Run Code Online (Sandbox Code Playgroud) 操作系统:Ubuntu
rustup update-->成功rutstup target install thumbv7m-none-eabi-->成功cargo install cargo-flasherror: failed to run custom build command for `hidapi v1.4.2`
process didn't exit successfully: `/tmp/cargo-installgobLzf/release/build/hidapi-aad2646622c847a4/build-script-build` (exit status: 101)
error: could not find system library 'libudev' required by the 'hidapi' crate
error: failed to compile `cargo-flash v0.13.0`, intermediate artifacts can be found at `/tmp/cargo-installgobLzf`
Run Code Online (Sandbox Code Playgroud) 我需要动态地使用不同名称来检测跨度。如何创建具有动态命名的跟踪范围?
use tracing; // 0.1.22
fn main() {
let name: &'static str = "foo bar";
let span = tracing::span!(tracing::Level::TRACE, name);
let span_handle = span.enter();
tracing::info!("Hello, world!");
drop(span_handle);
}
Run Code Online (Sandbox Code Playgroud)
error[E0435]: attempt to use a non-constant value in a constant
--> src/main.rs:5:54
|
5 | let span = tracing::span!(tracing::Level::TRACE, name);
| ^^^^ non-constant value
Run Code Online (Sandbox Code Playgroud)
rust ×10
rust-tracing ×2
actix-web ×1
file ×1
lifetime ×1
ordered-map ×1
ordered-set ×1
raku ×1
rust-axum ×1
rust-cargo ×1
rust-polars ×1
rust-tokio ×1
stm32 ×1
trace ×1
vector ×1