我有
hyper = "0.10"
Run Code Online (Sandbox Code Playgroud)
以下代码:
let client = Client::new();
let mut res = client.get("https://google.com").send().unwrap();
Run Code Online (Sandbox Code Playgroud)
Rust给我错误消息,好像它没有SSL支持:
Http的方案无效
这是在Debian jessie的Rust 1.14.0上.
如何让Hyper将SSL连接到HTTPS URL?
我有一个长时间运行的子进程,我需要读取和写入大量数据。我有一个读取器线程和一个写入器线程,分别操作child.stdout
和child.stdin
:
extern crate scoped_threadpool;
fn main() {
// run the subprocess
let mut child = std::process::Command::new("cat")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap();
let child_stdout = child.stdout.as_mut().unwrap();
let child_stdin = std::sync::Mutex::new(child.stdin.as_mut().unwrap());
let mut pool = scoped_threadpool::Pool::new(2);
pool.scoped(|scope| {
// read all output from the subprocess
scope.execute(move || {
use std::io::BufRead;
let reader = std::io::BufReader::new(child_stdout);
for line in reader.lines() {
println!("{}", line.unwrap());
}
});
// write to the subprocess
scope.execute(move || {
for a in 0..1000 {
use std::io::Write;
writeln!(&mut …
Run Code Online (Sandbox Code Playgroud) 我的程序使用rusqlite从另一个数据源构建数据库.数据库以相同的方式构建多个表,所以我想我会创建一个可重用的函数:
fn download_generic<Inserter>(table_name: &str,
connection: &mut rusqlite::Connection,
inserter: &mut Inserter)
-> Result<(), String>
where Inserter: FnMut(&str, &json::JsonValue) -> ()
{}
Run Code Online (Sandbox Code Playgroud)
inserter
是一个函数,它绑定先前准备的语句中的正确值并进行插入.
我称之为:
let mut insert_stmt = connection
.prepare("insert or replace into categories values(?,?);")
.unwrap();
download_generic("categories",
&mut connection,
&mut |uuid, jsonproperties| {
insert_stmt.execute(&[&uuid, &jsonproperties["name"].as_str().unwrap_or("")]);
});
Run Code Online (Sandbox Code Playgroud)
但是我无法传递&mut connection
,download_generic
因为它已经被借来了insert_stmt
.将它放入一个RefCell
没有意义,因为我不需要运行时开销来使这项工作.
我可以尝试insert_stmt
通过你传递给它的lambda来生成download_generic
,但是后来我不得不在任何地方添加生命周期标记而感到不知所措,无论如何它似乎都不自然.
我有一个程序可以缓慢地生成数据(我们可以说它是计算密集型的,就像计算 pi 的数字一样)。它产生大量数据;每个响应可以是 1GiB,不适合内存,并且必须按需生成。我正在使用 hyper 编写一个 Web 服务来根据请求生成内容。
让我们跳过样板(service_fn
, Server::bind
)。
缓慢生成数据的 API 可能类似于
use std::io;
impl SlowData {
fn new(initial: &str) -> SlowData {
unimplemented!()
}
fn next_block(&self) -> io::Result<&[u8]> {
unimplemented!()
}
}
type ResponseFuture = Box<Future<Item = Response, Error = GenericError> + Send>;
fn run(req: Request) -> ResponseFuture {
// spawn a thread and:
// initialize the generator
// SlowData::new(&req.uri().path());
// spawn a thread and call slow.next_block() until len()==0
// each …
Run Code Online (Sandbox Code Playgroud) 如您所知,for in
如果直接向循环传递迭代器,则循环在循环持续时间内拥有其迭代器,如下所示:
let v = vec![...];
let mut i = v.iter();
for _ in i { }
Run Code Online (Sandbox Code Playgroud)
正如 malbarbo 所观察到的,您可以通过编写 来指示for
引用。但是,您不能在 for 循环内部重复该操作:i
i.by_ref()
for _ in i.by_ref() {
for _ in i.by_ref() {
// ^ error: cannot borrow `i` as mutable
// more than once at a time [--explain E0499]
break;
}
}
Run Code Online (Sandbox Code Playgroud)
可以理解的是,外部for
循环必须修改其迭代器,因此它需要对其进行可变引用,并且其他人不能再调用可变方法i
。我们可以更直接地展示这个问题,如下所示:
for _ in i.by_ref() {
i.next(); // same error
}
Run Code Online (Sandbox Code Playgroud)
一种办法是制作外部for
aloop
并i.next() …
我有一个 PostgreSQL 表,看起来像这样:
CREATE TABLE items (
name TEXT NOT NULL,
value TEXT NOT NULL,
PRIMARY KEY (name, value)
);
Run Code Online (Sandbox Code Playgroud)
我经常做一个查询,看看有什么value
可用的:
SELECT DISTINCT value FROM items;
Run Code Online (Sandbox Code Playgroud)
如何在 PostgreSQL 中创建索引,使上述查询不必遍历所有items
表?
我将数据解析为:
struct Data {
field1: Option<f32>,
field2: Option<u64>,
// more ...
}
Run Code Online (Sandbox Code Playgroud)
问题是我的输入数据格式将None
Rust 中的 a 格式化为“ n/a
”。
如何告诉 Serde anOption<T>
应该代表None
特定的 string n/a
,而不是错误?我们可以假设这不适用于String
.
这与How to deserialize "NaN" as `nan` with serde_json?不是同一个问题。因为这是从一个特殊值创建一个f32
,而我的问题是从一个特殊值创建一个Option<Anything>
。这也不是如何使用 Serde 反序列化期间转换字段?因为这仍然涉及特定类型。