rust

Deleting from an associated table with a subquery using Diesel from a postgres database

做~自己de王妃 提交于 2020-04-14 08:59:09
问题 I have a query that I am trying to translate from SQL into rust/diesel but am running into issues with creating a subquery using diesel. I am using diesel = "1.4.2" along with the postgres feature. I have the following schema and models... #[macro_use] extern crate diesel; mod schema { table! { jobs (id) { id -> Int4, } appointments (id) { id -> Int4, } categories (id) { id -> Int4 } appointments_categories(appointment_id, category_id) { appointment_id -> Int4, category_id -> Int4 } } } mod

Rust Chrono parse date String, ParseError(NotEnough) and ParseError(TooShort)

倖福魔咒の 提交于 2020-04-14 06:17:46
问题 How to convert a String to a chrono::DateTime or chrono::NaiveDateTime And what does the ParseError(NotEnough) or ParseError(TooShort) mean? 回答1: When converting a String into a Chrono object you have to know what parts the input format of the string has. The parts are: Date, Time, TimeZone Examples: "2020-04-12" => Date = NaiveDate "22:10" => Time = NaiveTime "2020-04-12 22:10:57" => Date + Time = NaiveDateTime "2020-04-12 22:10:57+02:00" => Date + Time + TimeZone = DateTime<Tz> The

In Rust, what's the idiomatic way to split a &str into an iterator of &strs of one character each?

烂漫一生 提交于 2020-04-13 14:52:16
问题 If I want to take a &str like "aeiou" and turn it into an iterator roughly equivalent to ["a", "e", "i", "o", "u"].iter() , what's the most idiomatic way to do it? I've tried doing "aeiou".split("") which seemed idiomatic to me, but I got empty &str s at the beginning and end. I've tried doing "aeiou".chars() but it got pretty ugly and unwieldy from there trying to turn the char s into &str s. For the time being, I just typed out ["a", "e", "i", "o", "u"].iter() , but there's got to be an

In Rust, what's the idiomatic way to split a &str into an iterator of &strs of one character each?

落爺英雄遲暮 提交于 2020-04-13 14:51:09
问题 If I want to take a &str like "aeiou" and turn it into an iterator roughly equivalent to ["a", "e", "i", "o", "u"].iter() , what's the most idiomatic way to do it? I've tried doing "aeiou".split("") which seemed idiomatic to me, but I got empty &str s at the beginning and end. I've tried doing "aeiou".chars() but it got pretty ugly and unwieldy from there trying to turn the char s into &str s. For the time being, I just typed out ["a", "e", "i", "o", "u"].iter() , but there's got to be an

Is transmuting bytes to a float safe or might it produce undefined behavior?

依然范特西╮ 提交于 2020-04-13 07:28:26
问题 Are there byte-sequences that, when transmuted into either f32 or f64 , produce undefined-behavior in Rust? I'm counting non-finite values, such as NaN, Infinity, etc. as valid floating point values. The comments to this answer hint that there may be some problem converting a float from raw bytes. 回答1: The Rust reference provides a good list of situations where undefined behavior occurs. Of those, the one that most closely relates to the question is the following: Invalid values in primitive

What does a trait requiring Sized have to do with being unable to have trait objects of that trait?

好久不见. 提交于 2020-04-13 03:54:11
问题 I have this code (playground): trait NodeLike: Sized {} fn main() { let s: Box<NodeLike> = panic!(); } Which does not compile: error[E0038]: the trait `NodeLike` cannot be made into an object --> src/main.rs:4:12 | 4 | let s: Box<NodeLike> = panic!(); | ^^^^^^^^^^^^^ the trait `NodeLike` cannot be made into an object | = note: the trait cannot require that `Self : Sized` After all I read, I still don't understand why it does not compile and why it does without the Sized constraint. As far as

Querying a Diesel table with dynamic parameters

笑着哭i 提交于 2020-04-13 01:04:20
问题 I was starting to look into using Diesel for querying a database. I have a table that looks something like the struct below (this is just a toy project to help me understand how Diesel works). #[derive(Queryable, Insertable)] #[table_name="posts"] struct Post { id: String, title: String, body: String, published: bool } Doing queries that are fully determined at compile time is easy enough, for example posts.select(id, title).order(title.desc()); What is not clear to me is how to build a query

How do I resolve “implementation of serde::Deserialize is not general enough” with actix-web's Json type?

跟風遠走 提交于 2020-04-12 19:44:37
问题 I'm writing a server using actix-web: use actix_web::{post, web, Responder}; use serde::Deserialize; #[derive(Deserialize)] struct UserModel<'a, 'b> { username: &'a str, password: &'b str, } #[post("/")] pub fn register(user_model: web::Json<UserModel>) -> impl Responder {} The compiler gives this error: error: implementation of `user::_IMPL_DESERIALIZE_FOR_UserModel::_serde::Deserialize` is not general enough --> src/user.rs:31:1 | 31 | #[post("/")] | ^^^^^^^^^^^^ | = note: `user::UserModel<

编程分号的简要历史,分号的使用来自何处?

筅森魡賤 提交于 2020-04-12 11:10:24
在本文中,我将简要介绍在计算机编程语言中使用分号(许多计算机科学入门学生的祸根)的历史。我们将看到分号有两种用途: 作为语句分隔符和作为语句终止符 ,并且我们将了解在过去60多年的编程语言历史中分号的使用发生了怎样的变化。 为什么编程语言使用分号 分号在编程语言中用于两件事:语句分隔符和语句终止符。当一种语言使用分号作为语句分隔符时,这允许您在同一行上编写多个语句,并使用分号来标记语句的分隔,以便编译器或解释器可以找出一个语句在哪里结束而另一条语句在哪里开始。 在以分号为分隔符的语言中,分号被视为可选的,当一行代码仅包含一条语句时,通常不会编写分号。 作为语句结束符的分号是不可选的,用于明确地标记语句的结束,使用分号作为终止符的编程语言将在分号不在其预期位置时标记错误。许多编程专家声称,以分号结尾的语句可以使程序更快,因为编译器可以更有效地执行,尽管我在本文末尾会对此主张提出异议。 早期的高级语言和分号的使用 当Fortran在20世纪50年代中期被作为一种高级编程语言引入时,编程语言的史前时代就结束了,Fortran既不使用分号作为语句分隔符,也不使用分号作为语句终止符,Fortran中的语句每行只能写一个,而新行是语句终止符。 在现代编程语言中首次使用分号是ALGOL 58,然而,这个版本并没有得到很好的实现,它很快就变成了ALGOL 60

Rust 1.42.0 发布

ぃ、小莉子 提交于 2020-04-10 09:50:59
https://www.oschina.net/news/114076/rust-1-42-0-released Rust 1.42.0 现已发布,该版本的亮点包括: unwrap ping 时更有用的紧急消息、分段模式、弃用 Error::description 等等。具体更新内容如下: Language 现在,用户可以将切片模式语法与子切片一起使用。 用户现在可以在单变量枚举上使用 #[repr(transparent)] ,这意味着其可以创建一个枚举,该枚举具有其所包含类型的确切布局和 ABI。 一些 syntax-only 更改: 在特性定义中,在句法之前允许使用 default 包含在 impls 中的项目(即 const,type 和 fns)在语法上可能会遗漏它们的主体以支持 ; . 现在,语法上允许在 impls 中关联类型的界限(例如 type Foo: Ord; )。 ... (C 变量类型)可以在语法上直接作为任何函数参数的类型出现。 这些仍然在语义上被拒绝,因此用户可能会收到一个错误,但是这些更改可以通过过程宏和条件编译来查看和解析。 Compiler 添加 tier 2* 以支持 armv7a-none-eabi 添加 tier 2 以支持 riscv64gc-unknown-linux-gnu Option::{expect,unwrap} 和