devirtualize:由于某些保证更改是正确的,将虚拟/多态/间接函数调用更改为静态函数调用 - 来源:我自己
给定一个&dyn ToString使用静态已知类型创建的简单特征对象String:
fn main() {
let name: &dyn ToString = &String::from("Steve");
println!("{}", name.to_string());
}
Run Code Online (Sandbox Code Playgroud)
请问直接调用.to_string()使用<String as ToString>::to_string()吗?还是仅通过 trait 的 vtable 间接?如果是间接的,是否可以将这个调用去虚拟化?或者有什么基本的东西阻碍了这种优化?
这个问题的激励代码要复杂得多;它使用异步特征函数,我想知道Box<dyn Future>在某些情况下是否可以优化返回 a 。
我正在尝试向 Binance API 发送 GET 请求。但我在终端中得到以下输出而不是数据:
Response { url: Url { scheme: "https", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("api.binance.com")), port: None, path: "/api/v3/exchangeInfo", query: Some("symbol=BNBBTC"), fragment: None }, status: 200, headers: {"content-type": "application/json;charset=UTF-8", "content-length": "1515", "connection": "keep-alive", "date": "Thu, 23 Dec 2021 23:28:24 GMT", "server": "nginx", "vary": "Accept-Encoding", "x-mbx-uuid": "1244d760-2c41-46df-910f-b95c4a312bc2", "x-mbx-used-weight": "10", "x-mbx-used-weight-1m": "10", "strict-transport-security": "max-age=31536000; includeSubdomains", "x-frame-options": "SAMEORIGIN", "x-xss-protection": "1; mode=block", "x-content-type-options": "nosniff", "content-security-policy": "default-src 'self'", "x-content-security-policy": "default-src 'self'", "x-webkit-csp": "default-src 'self'", "cache-control": "no-cache, no-store, must-revalidate", "pragma": …Run Code Online (Sandbox Code Playgroud) 我无法理解为什么这会导致错误:
#[derive(Debug)]
pub struct Node {
next: Option<Box<Node>>,
}
pub fn print_root_or_next(root: &mut Node, try_next: bool) {
let mut current = root;
match &mut current.next {
Some(node) => {
if try_next {
current = &mut *node;
}
}
None => return,
}
println!("{:?}", current);
}
Run Code Online (Sandbox Code Playgroud)
#[derive(Debug)]
pub struct Node {
next: Option<Box<Node>>,
}
pub fn print_root_or_next(root: &mut Node, try_next: bool) {
let mut current = root;
match &mut current.next {
Some(node) => {
if try_next {
current = …Run Code Online (Sandbox Code Playgroud) 有没有办法记录 actix-web 收到的所有请求,无论端点是否存在?
看来我需要为此使用中间件,这是推荐的方法吗?
为了让我的代码更简洁,我尝试通过名称直接将闭包传递map()给&[&str]. map 需要迭代&&str这是可以理解的,但我的闭包只需要&str.
代码示例:
fn capitalize_first(word: &str) -> String {
word.chars().next().unwrap().to_uppercase().to_string()
}
fn main() {
let cap_cap = |word: &&str| -> String {
word.chars().next().unwrap().to_uppercase().to_string()
};
let a = ["we", "are", "testing"];
let b = &a; // this is where this becomes interesting.
b.iter().map(|&x| capitalize_first(x)); // Works
b.iter().map(|x| capitalize_first(x)); // Works
b.iter().map(cap_cap); // That's what the compiler expects.
b.iter().map(capitalize_first); // fails to compile!
}
Run Code Online (Sandbox Code Playgroud)
编译器错误:
error[E0631]: type mismatch in function arguments
--> src/main.rs:17:18 …Run Code Online (Sandbox Code Playgroud) fn main() {
let arr: [u8;8] = [97, 112, 112, 108, 101];
println!("Len is {}",arr.len());
println!("Elements are {:?}",arr);
}
Run Code Online (Sandbox Code Playgroud)
fn main() {
let arr: [u8;8] = [97, 112, 112, 108, 101];
println!("Len is {}",arr.len());
println!("Elements are {:?}",arr);
}
Run Code Online (Sandbox Code Playgroud)
有什么办法可以用0来填充剩余的元素吗?就像是:
let arr: [u8;8] = [97, 112, 112, 108, 101].something();
Run Code Online (Sandbox Code Playgroud) 正如标题所示,在 Rust 中,.rev().rev()有效, .rev().skip(1)有效,但.rev().skip(1).rev()无效。下面是演示:
// This compiles
fn main() {
let s = "Hello!";
println!("{}", &s.chars().rev().skip(1).collect::<String>());
}
Run Code Online (Sandbox Code Playgroud)
// This compiles
fn main() {
let s = "Hello!";
println!("{}", &s.chars().rev().rev().collect::<String>());
}
Run Code Online (Sandbox Code Playgroud)
// This *does not* compile
fn main() {
let s = "Hello!";
println!("{}", &s.chars().rev().skip(1).rev().collect::<String>());
}
Run Code Online (Sandbox Code Playgroud)
最后一个无法编译:
// This compiles
fn main() {
let s = "Hello!";
println!("{}", &s.chars().rev().skip(1).collect::<String>());
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么会出现这种情况吗?
我正在研究 Rust 并查看 Yew 框架(Rust 的前端框架)的这些简单说明: https: //yew.rs/docs/tutorial。
\n我按照说明进行操作,直到收到命令
\ntrunk serve --open\nRun Code Online (Sandbox Code Playgroud)\n然而,某处出现故障。
\n这就是我得到的:
\ntrunk serve --open\nRun Code Online (Sandbox Code Playgroud)\n这是我的 Cargo.toml 文件:
\n2022-05-06T19:07:54.087214Z INFO starting build\n2022-05-06T19:07:54.087870Z INFO spawning asset pipelines\n2022-05-06T19:07:54.168329Z INFO building yew-app\n Finished dev [unoptimized + debuginfo] target(s) in 0.01s\n2022-05-06T19:07:54.232154Z INFO fetching cargo artifacts\n2022-05-06T19:07:54.295124Z INFO processing WASM for yew-app\n2022-05-06T19:07:54.301974Z INFO downloading wasm-bindgen version="0.2.80"\n2022-05-06T19:07:54.302269Z ERROR \xe2\x9d\x8c error\nerror from HTML pipeline\n\nCaused by:\n 0: error from asset pipeline\n 1: failed downloading release archive\n 2: unsupported architecture\n2022-05-06T19:07:54.302531Z …Run Code Online (Sandbox Code Playgroud) 我将 Axum 用于相对简单的 Web API,并希望获得类似于 Go Gin、IIS 日志、Python FastAPI 等的传入请求的日志记录/跟踪输出 - 简单的路径和参数输出。
HTTP 层已添加到路由器:
let app = Router::new()
.route("/hello", get(hello_img))
.layer(TraceLayer::new_for_http());
Run Code Online (Sandbox Code Playgroud)
然而,还有很多额外的不需要的日志记录正在发生,所以我添加了一个过滤器来排除这些日志记录。添加过滤器后:
let filter = filter::Targets::new()
.with_target("tower_http::trace::on_response", Level::TRACE)
.with_target("tower_http::trace::on_request", Level::TRACE)
.with_default(Level::INFO);
Run Code Online (Sandbox Code Playgroud)
并将其添加到订阅者:
let tracing_layer = tracing_subscriber::fmt::layer();
tracing_subscriber::registry()
.with(tracing_layer)
.with(filter)
.init();
Run Code Online (Sandbox Code Playgroud)
详细信息(方法、URI、参数)都消失了。
即使没有指定格式更改,为什么会发生这种情况?如何在控制台中保留请求/响应跟踪,但过滤掉其他不需要的跟踪?
我是docker新手,第一次遇到这样的错误。
这是我的 DockerFile
FROM rust:latest as builder
ENV APP mapservice
WORKDIR /usr/src/$APP
COPY . .
RUN cargo install --path .
FROM debian:buster-slim
RUN apt-get update && rm -rf /var/lib/apt/lists/*
COPY --from=builder /usr/local/cargo/bin/$APP /usr/local/bin/$APP
#export this actix web service to port 8080 and 0.0.0.0
EXPOSE 8080
CMD ["mapservice"]
Run Code Online (Sandbox Code Playgroud)
当我跑步时
docker run -it --rm -p 8080:8080 mapservice
Run Code Online (Sandbox Code Playgroud)
我收到如下错误:
mapservice: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory
我不知道为什么会出现这个错误。也许我的 APIKEY 硬编码在 main.rs 中?有人知道如何解决这个问题吗?我的笔记本电脑是 M1pro Mac。 …
rust ×9
actix-web ×1
docker ×1
dockerfile ×1
reqwest ×1
rust-axum ×1
rust-macros ×1
rust-tracing ×1
trunk-rs ×1
wasm-bindgen ×1
webassembly ×1
yew ×1