我有一个我希望通过Diesel使用的SQL表:
CREATE TABLE records (
id BIGSERIAL PRIMARY KEY,
record_type SMALLINT NOT NULL,
value DECIMAL(10, 10) NOT NULL
)
Run Code Online (Sandbox Code Playgroud)
该表生成以下模式:
table! {
records (id) {
id -> Int8,
record_type -> Int2,
value -> Numeric,
}
}
Run Code Online (Sandbox Code Playgroud)
Diesel将小数字输出为bigdecimal::BigDecimal,但我想decimal::d128改为使用.我也想映射record_type到枚举,所以我声明我的模型如下:
use decimal::d128;
pub enum RecordType {
A,
B,
}
pub struct Record {
pub id: i64,
pub record_type: RecordType,
pub value: d128,
}
Run Code Online (Sandbox Code Playgroud)
我不能使用#derive(Queryable, Insertable)因为非标准类型映射,所以我尝试自己实现这些特征:
impl Queryable<records::SqlType, Pg> for Record {
type Row = (i64, …Run Code Online (Sandbox Code Playgroud) 当我定义以下结构时:
struct Test<'a> {
a: &'a [i64],
b: Vec<i64>,
}
Run Code Online (Sandbox Code Playgroud)
切片和向量都包含指针.为什么切片需要一生,但不是矢量?
Serde支持应用用于以下内容的自定义属性#[derive(Serialize)]:
#[derive(Serialize)]
struct Resource {
// Always serialized.
name: String,
// Never serialized.
#[serde(skip_serializing)]
hash: String,
// Use a method to decide whether the field should be skipped.
#[serde(skip_serializing_if = "Map::is_empty")]
metadata: Map<String, String>,
}
Run Code Online (Sandbox Code Playgroud)
我理解如何实现一个过程宏(Serialize在这个例子中),但我该怎么做才能实现#[serde(skip_serializing)]?我无法在任何地方找到这些信息.该文档甚至没有提到这一点.我试着看一下serde-derive源代码,但对我来说这很复杂.
根据其手册,Cargo软件包可以具有多个可执行目标,但只允许一个库目标。
一个包可以包含零个或一个库箱,也可以包含任意数量的二进制箱。包中必须至少有一个板条箱(库或二进制文件)。
为什么只限于一个?原因和好处是什么?
我想返回对集合中拥有的对象的引用(即 a Vec),但我似乎无法获得正确的生命周期。这是我第一次尝试的:
struct StringHolder {
strings: Vec<String>,
i: usize,
}
impl Iterator for StringHolder {
type Item<'a> = &'a String;
fn next(&mut self) -> Option<Self::Item> {
if self.i >= self.strings.len() {
None
} else {
self.i += 1;
Some(&self.strings[self.i])
}
}
}
fn main() {
let sh = StringHolder { strings: vec![], i: 0 };
for string in sh {
println!("{}", string);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误,generic associated types are unstable并且lifetimes do not match type in trait …
我想玩一些旧的Collatz猜想并决定以(非常)功能的方式进行它很有趣,所以我实现了一个unfoldr函数,接近Haskell的一个:
fn unfoldr<F, T>(foo: F, seed: T, mut vec: Vec<T>) -> Vec<T>
where F: Fn(T) -> Option<(T, T)>
{
if let Some((x, y)) = foo(seed) {
vec.push(x);
unfoldr(foo, y, vec)
} else {
vec
}
}
Run Code Online (Sandbox Code Playgroud)
其余的非常简单:
fn collatz_next(n: u64) -> u64 {
if n % 2 == 0 { n / 2 } else { 3 * n + 1 }
}
pub fn collatz_seq_f(n: u64) -> Vec<u64> {
unfoldr(|n| if n == …Run Code Online (Sandbox Code Playgroud) 我有一些由bindgen生成的代码,其中包含由函数指针表示的函数Option<T>.
pub type SomeFnPointerType = ::std::option::Option<
unsafe extern "C" fn ( /* .. large number of argument types .. */)
-> /* return type */
> ;
Run Code Online (Sandbox Code Playgroud)
我想将未包装的值存储在结构中.但是,内部函数指针类型没有类型别名,那么如何定义该结构呢?我无法重构代码,因为它是自动生成的.
我想做这样的事情(用C++'ish伪代码):
struct FunctionTable {
decltype((None as SomeFnPointerType).unwrap()) unwrappedFn;
/* ... */
};
Run Code Online (Sandbox Code Playgroud)
或者也许只是SomeFnPointerType::T在允许的情况下.
在Rust中可以实现吗?如果没有,我看到的唯一方法是通过将生成的文件中的代码复制粘贴到单独的手写文件中手动定义这些类型,并手动保持类型同步.
我正在使用Rust中的二进制序列化和反序列化,并注意到二进制反序列化比使用Java慢几个数量级.为了消除由于例如分配和开销导致的开销的可能性,我只是从每个程序中读取二进制流.每个程序从磁盘上的二进制文件读取,该文件包含一个包含输入值数量的4字节整数,以及一个8字节大端IEEE 754编码浮点数的连续块.这是Java实现:
import java.io.*;
public class ReadBinary {
public static void main(String[] args) throws Exception {
DataInputStream input = new DataInputStream(new BufferedInputStream(new FileInputStream(args[0])));
int inputLength = input.readInt();
System.out.println("input length: " + inputLength);
try {
for (int i = 0; i < inputLength; i++) {
double d = input.readDouble();
if (i == inputLength - 1) {
System.out.println(d);
}
}
} finally {
input.close()
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是Rust的实现:
fn main() {
use std::os;
use std::io::File;
use std::io::BufferedReader;
let …Run Code Online (Sandbox Code Playgroud) 我仍在尝试了解 Rust 所有权和生命周期,并且我对这段代码感到困惑:
struct Foo {
x: String,
}
fn get_x<'a, 'b>(a: &'a Foo, b: &'b Foo) -> &'b str {
let mut bar = &a.x;
bar = &b.x;
bar
}
Run Code Online (Sandbox Code Playgroud)
该代码无法编译,因为data from 'a' is returned. 我认为这是因为当我初始化时bar,我分配了一个&'a对它的引用,所以 Rust 假设它bar有生命周期'a。因此,当我尝试返回 type 的值时,&'a str它会抱怨它与返回类型不匹配'b str。
我不明白的是:为什么我首先可以分配 a&'b str到?bar如果 Rust 假设bar具有生命周期'a,那么它不应该阻止我分配b.x给它吗?
rust ×10
lifetime ×2
file-io ×1
iterator ×1
optimization ×1
performance ×1
rust-cargo ×1
rust-diesel ×1