rust

cannot borrow `*self` as mutable because it is also borrowed as immutable

好久不见. 提交于 2021-02-07 07:01:23
问题 I want my struct function to call itself under special conditions. It worked when I had a HashMap as one of the fields, but it broke when I changed the HashMap to be a Vec . It doesn't even have to be used, which seems very weird and I can't find any reasonable explanation for this. use std::vec::Vec; use std::collections::HashMap; struct Foo<'a> { bar: Vec<&'a str> //bar: HashMap<&'a str, &'a str> } impl<'a> Foo<'a> { pub fn new() -> Foo<'a> { Foo { bar: Vec::new() } //Foo { bar: HashMap:

Cannot infer type for `U`

拈花ヽ惹草 提交于 2021-02-07 06:49:13
问题 I am using Rust and Diesel: fn create_asset_from_object(assets: &HashMap<String, Assets_Json>) { let connection: PgConnection = establish_connection(); println!("=========================================================="); insert_Asset(&connection, &assets); } pub fn insert_Asset(conn: &PgConnection, assests: &HashMap<String, Assets_Json>){ use self::schema::assets; for (currency, assetInfo) in assests { let new_asset = self::models::NewAssets { asset_name: &currency, aclass: &assetInfo

How do I access exported functions inside a crate's “tests” directory?

南笙酒味 提交于 2021-02-07 04:18:47
问题 How do I access my libraries exported functions inside the create's "tests" directory? src/relations.rs: #![crate_type = "lib"] mod relations { pub fn foo() { println!("foo"); } } tests/test.rs: use relations::foo; #[test] fn first() { foo(); } $ cargo test Compiling relations v0.0.1 (file:///home/chris/github/relations) /home/chris/github/relations/tests/test.rs:1:5: 1:14 error: unresolved import `relations::foo`. Maybe a missing `extern crate relations`? /home/chris/github/relations/tests

Why use `ptr::write` with arrays of `MaybeUninit`s?

旧街凉风 提交于 2021-02-06 15:53:13
问题 In the standard library, the documentation shows how to instantiate arrays of MaybeUninit s: let arr: [MaybeUninit<T>; N] = MaybeUninit::uninit().assume_init(); We know this is safe because the contract of MaybeUninit allows for uninitialized values. Next we are asked to use ptr::write(value) to initialize each element. But this requires unsafe code once again. We also know that overwriting a MaybeUninit is safe, because it doesn't drop anything. So why not just overwrite it like arr[i] =

What does the first explicit lifetime specifier on an impl mean?

我与影子孤独终老i 提交于 2021-02-06 10:13:45
问题 There are three different lifetime specifiers on an impl: impl<'a> Type<'a> { fn my_function(&self) -> &'a u32 { self.x } } Type<'a> states that there is a lifetime in this impl declaration. The one on the return type -> &'a u32 states that the variable that receives the return value should not die before...before what? Before the object of type Type dies? What's the difference to this: impl TextEditor { //Other methods omitted ... pub fn get_text<'a>(&'a self) -> &'a String { return &self

What is the difference between how references and Box<T> are represented in memory?

喜你入骨 提交于 2021-02-06 10:13:39
问题 I am trying to understand how references and Box<T> work. Let's consider a code example: fn main() { let x = 5; let y = &x; assert_eq!(5, x); assert_eq!(5, *y); } In my imagination, Rust saves the value in memory as: Consider this second code snippet with Box<T> : fn main() { let x = 5; let y = Box::new(x); assert_eq!(5, x); assert_eq!(5, *y); } How is x going to be stored in Box ? What does the memory look like? The examples above are from Treating Smart Pointers Like Regular References with

What is the difference between how references and Box<T> are represented in memory?

你说的曾经没有我的故事 提交于 2021-02-06 10:13:27
问题 I am trying to understand how references and Box<T> work. Let's consider a code example: fn main() { let x = 5; let y = &x; assert_eq!(5, x); assert_eq!(5, *y); } In my imagination, Rust saves the value in memory as: Consider this second code snippet with Box<T> : fn main() { let x = 5; let y = Box::new(x); assert_eq!(5, x); assert_eq!(5, *y); } How is x going to be stored in Box ? What does the memory look like? The examples above are from Treating Smart Pointers Like Regular References with

What does the first explicit lifetime specifier on an impl mean?

血红的双手。 提交于 2021-02-06 10:05:38
问题 There are three different lifetime specifiers on an impl: impl<'a> Type<'a> { fn my_function(&self) -> &'a u32 { self.x } } Type<'a> states that there is a lifetime in this impl declaration. The one on the return type -> &'a u32 states that the variable that receives the return value should not die before...before what? Before the object of type Type dies? What's the difference to this: impl TextEditor { //Other methods omitted ... pub fn get_text<'a>(&'a self) -> &'a String { return &self

What does the first explicit lifetime specifier on an impl mean?

故事扮演 提交于 2021-02-06 10:04:24
问题 There are three different lifetime specifiers on an impl: impl<'a> Type<'a> { fn my_function(&self) -> &'a u32 { self.x } } Type<'a> states that there is a lifetime in this impl declaration. The one on the return type -> &'a u32 states that the variable that receives the return value should not die before...before what? Before the object of type Type dies? What's the difference to this: impl TextEditor { //Other methods omitted ... pub fn get_text<'a>(&'a self) -> &'a String { return &self

Why does Rust allow calling functions via null pointers?

人走茶凉 提交于 2021-02-06 06:30:55
问题 I was experimenting with function pointer magic in Rust and ended up with a code snippet which I have absolutely no explanation for why it compiles and even more, why it runs. fn foo() { println!("This is really weird..."); } fn caller<F>() where F: FnMut() { let closure_ptr = 0 as *mut F; let closure = unsafe { &mut *closure_ptr }; closure(); } fn create<F>(_: F) where F: FnMut() { caller::<F>(); } fn main() { create(foo); create(|| println!("Okay...")); let val = 42; create(|| println!(