rust

the trait `diesel::Expression` is not implemented for `bigdecimal::BigDecimal`

回眸只為那壹抹淺笑 提交于 2020-05-29 03:53:46
问题 I am trying to create a struct that I can use in diesel for insertion. Specifically I am making the struct Insertable. On compile I get this error. I have a struct that I am trying to make Insertable via the derive attribute. I have a field called Bounty which is supposed to represent money, so I'm using BigDecimal as the type. Upon compilation, I get the error in the title. I've also tried using f64 but that gives the same error. #[macro_use] extern crate diesel; extern crate bigdecimal; mod

How do intertwined scopes create a “data race”? [duplicate]

孤街醉人 提交于 2020-05-28 06:59:20
问题 This question already has answers here : Why does Rust disallow mutable aliasing? (3 answers) Closed 10 days ago . The Rust book talks about having multiple readers and multiple mutable references to an object as a data race situation that may lead to issues. For example, this code: fn main() { let mut x = 1; let r1 = &mut x; *r1 = 2; let r2 = &mut x; *r2 = 3; println!("{}", r1); println!("{}", r2); } will be rejected by Rust compiler because r1 and r2 scopes are intertwined. But what is

How to use function return value directly in Rust println

百般思念 提交于 2020-05-28 05:38:06
问题 Rust allows formatted printing of variables this way: fn main(){ let r:f64 = rand::random(); println!("{}",r); } But this doesn't work: fn main(){ println!("{}",rand::random()); } It shows up this error: | 31 | println!("{}",rand::random()); | ^^^^^^^^^^^^ cannot infer type for type parameter `T` declared on the function `random` Is it possible to use function return value directly with println! ? 回答1: Rust doesn't know what type rand::random should be, so you can use the turbofish to provide

Alternatives for self-referential structs in Rust?

跟風遠走 提交于 2020-05-27 12:31:37
问题 There are tons of questions about self-referential structs in Rust here, and I think I've read them all, but I'm still having trouble wrapping my head around things. What kind of design patterns exist for dealing with self-referential structs in Rust? I'll lay out my thinking so that others can tell me where I'm going wrong (I have a feeling it's at the beginning). When I'm learning a new language I try to implement the same game, and for Rust I'm running into this problem: we have resources,

Using Cargo with my project's own directory structure

末鹿安然 提交于 2020-05-27 12:14:19
问题 Can I use Cargo to build Rust code without using its standard project layout? My source files are not in a directory called src and this will not change. My binaries must end up in the current directory (or, in some other projects, in a different directory that is not called target/SOMETHING ). Can I tell Cargo that executable foo must be built from foo.rs and bar.rs in the same directory as Cargo.toml , and qux from foo.rs ? I don't care about Cargo as a build system or as a deployment

Using Cargo with my project's own directory structure

人走茶凉 提交于 2020-05-27 12:13:26
问题 Can I use Cargo to build Rust code without using its standard project layout? My source files are not in a directory called src and this will not change. My binaries must end up in the current directory (or, in some other projects, in a different directory that is not called target/SOMETHING ). Can I tell Cargo that executable foo must be built from foo.rs and bar.rs in the same directory as Cargo.toml , and qux from foo.rs ? I don't care about Cargo as a build system or as a deployment

How to access value in RefCell properly

老子叫甜甜 提交于 2020-05-27 04:43:00
问题 I'm trying to wrap my head around Rc and RefCell in Rust. What I'm trying to achieve is to to have multiple mutable references to the same objects. I came up with this dummy code: use std::rc::Rc; use std::cell::RefCell; struct Person { name: String, mother: Option<Rc<RefCell<Person>>>, father: Option<Rc<RefCell<Person>>>, partner: Option<Rc<RefCell<Person>>> } pub fn main () { let mut susan = Person { name: "Susan".to_string(), mother: None, father: None, partner: None }; let mut boxed_susan

How to access value in RefCell properly

牧云@^-^@ 提交于 2020-05-27 04:42:31
问题 I'm trying to wrap my head around Rc and RefCell in Rust. What I'm trying to achieve is to to have multiple mutable references to the same objects. I came up with this dummy code: use std::rc::Rc; use std::cell::RefCell; struct Person { name: String, mother: Option<Rc<RefCell<Person>>>, father: Option<Rc<RefCell<Person>>>, partner: Option<Rc<RefCell<Person>>> } pub fn main () { let mut susan = Person { name: "Susan".to_string(), mother: None, father: None, partner: None }; let mut boxed_susan

How do I set the lifetime of a return value as the lifetime of the variable I move into it?

a 夏天 提交于 2020-05-26 11:24:00
问题 I am trying to teach myself some rust, and have written something that looks like: let args:Vec<String> = env::args().collect(); let parsed = parser::sys(args.as_slice()); ... pub fn sys<'a>(args:&'a [String]) -> Parsed<'a> { parsed(args) } where parsed is a function that parses and loads configs. This works fine. Now I am trying to abstract away explicitly calling env::args() and hide it in the call to sys , so I write a new version of sys pub fn sys<'a>() -> Parsed<'a> { let args:Vec<String

Confusion between [T] and &[T]

霸气de小男生 提交于 2020-05-26 11:23:20
问题 I'm currently confused by [T] and &[T] in Rust. Let's start by what I know: [T; n] is an array of n elements, &[T; n] is a pointer to an array with n elements, [T] is unsized and points to sequence of elements T , while &[T] is a sized fat pointer and points to a sequence of elements T . My confusion starts with the naming convention of the two items. From the documentation of Rust, they provide the following example: let a: [i32; 5] = [1, 2, 3, 4, 5]; // An array of type [T, n] let slice: &