我正在尝试在 Rust 中编写我自己的派生模式宏,并且它的文档在示例中有些缺乏。
我有一个像这样的结构:
#[derive(MyMacroHere)]
struct Example {
id: i64,
value: Option<String>,
}
Run Code Online (Sandbox Code Playgroud)
我希望我的宏生成一个方法 à la
fn set_fields(&mut self, id: i64, value: Option<String>) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
使用TokenStream特征来实现类似目标的基本步骤是什么?
有一个像这样的简单表结构:
CREATE TABLE test (
id INT PRIMARY KEY,
sid SERIAL
);
Run Code Online (Sandbox Code Playgroud)
我注意到如果我尝试插入一行但它没有通过约束测试(即PRIMARY KEY约束),SERIAL计数器将无论如何都会增加,因此下一次成功插入sid将sid + 2代替sid + 1.
这是正常的行为吗?有什么办法可以防止这个?
鉴于这个例子:
fn function() -> Result<(), &'static str> {
Ok(())
}
fn main() {
function()?; // Compile error
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:cannot use the ? operator in a function that returns ().
为什么我不能使用?运算符来执行这些功能?是否存在语法糖以避免使用match语句?
我有这个结构:
#[table_name = "clients"]
#[derive(Serialize, Deserialize, Queryable, Insertable, Identifiable, Associations)]
pub struct Client {
pub id: Option<i64>,
pub name: String,
pub rank: Option<i64>,
}
Run Code Online (Sandbox Code Playgroud)
以及以下实现:
impl Client {
pub fn get(name: String, connection: &PgConnection) -> Option<Self> {
match clients::table
.filter(clients::name.eq(&name))
.limit(1)
.load::<Client>(connection)
{
Ok(clients) => Some(clients[0]),
Err(_) => None,
}
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误:
#[table_name = "clients"]
#[derive(Serialize, Deserialize, Queryable, Insertable, Identifiable, Associations)]
pub struct Client {
pub id: Option<i64>,
pub name: String,
pub rank: Option<i64>,
}
Run Code Online (Sandbox Code Playgroud) 如果Mutex<T>设计Arc<T>使用a的唯一原因Mutex<T>是用于并发代码(即多个线程),为什么还要设计一个?首先将a别名Mutex<T>为原子引用会更好吗?我正在使用https://doc.rust-lang.org/book/ch16-03-shared-state.html作为参考。
我有一个内存分配问题,其中std::shared_ptr两次分配:
#include <memory>
#include <vector>
std::vector<std::shared_ptr<int>> list;
std::shared_ptr<int> test (int i) {
list.push_back(std::make_shared<int>(i));
return list.back();
}
int main() {
std::shared_ptr<int> a = test(5);
}
Run Code Online (Sandbox Code Playgroud)
Valgrind输出:
==28524== HEAP SUMMARY:
==28524== in use at exit: 0 bytes in 0 blocks
==28524== total heap usage: 2 allocs, 2 frees, 48 bytes allocated
==28524==
几乎所有std容器都会发生这种情况.我只打了std::make_shared一次电话.
当我只运行std::make_shared一次时,为什么会得到2个分配?如果可能,我如何只有1个分配?这是正确的编码还是我可以提高效率?
通过设置overflow: hidden图像容器的属性,可以很容易地从底部、左侧或右侧裁剪图像。
<div class="img-container">
<img class="img" src="/img.jpg" />
</div>
<style>
.img-container {
overflow: hidden;
max-height: 700px;
}
.img {
width: 100%;
height: auto;
}
</style>
Run Code Online (Sandbox Code Playgroud)
有没有办法从顶部裁剪图像?如果正在调整窗口大小并且图像不再适合容器的高度,则应从顶部而不是从底部裁剪图像。
鉴于这种:
fn main() {
let variable = [0; 15];
}
Run Code Online (Sandbox Code Playgroud)
Rust编译器生成此警告:
= note: #[warn(unused_variables)] on by default
= note: to avoid this warning, consider using `_variable` instead
Run Code Online (Sandbox Code Playgroud)
variable和之间有什么区别_variable?
在 C++ 中,我们可以这样做:
std::string ar[2] = {std::string("hello"), std::string("world")};
std::string hello = ar[0];
std::string world = ar[1];
Run Code Online (Sandbox Code Playgroud)
和hello最终world成为新的字符串。我认为这是因为=operator它的实现就像clone()Rust 中的 a 一样。
在 Rust 中,这看起来像这样:
let ar = vec![String::from("hello"), String::from("world")]
let hello = ar[0].clone();
let world = ar[1].clone();
Run Code Online (Sandbox Code Playgroud)
这很快就会变得非常重复。
是否可以克隆=对象,这看起来像是自然行为?
既然std::set是作为二叉树实现的,它如何比较std::string不等式?看起来像a < b && b < a吗?
是直接使用字符串的长度还是以某种方式对其进行散列?它是否完全保证字符串的唯一性?
如果我有:
struct Bar {}
struct Foo {
bar: Bar
}
impl Drop for Foo {
fn drop(&mut self) {}
}
impl Drop for Bar {
fn drop(&mut self) {}
}
fn main() {
let x = Foo { bar: Bar {} };
}
Run Code Online (Sandbox Code Playgroud)
首先调用哪个 drop,是 drop forFoo还是 drop for Bar?
rust ×7
c++ ×2
css ×1
mutex ×1
postgresql ×1
rust-diesel ×1
rust-macros ×1
sql ×1
stdset ×1
stdstring ×1