id
stringlengths
16
145
text
stringlengths
1
179k
title
stringclasses
1 value
r_std_hashmap/614907_24_0.txt
So pretty much what I want to do is, I create a list of those posts, and then I want to create a file with a specific format. This is why I can have the list of all symbols (tags) seen across the different posts. Now I guess I have 2 choices: 1. (feels more natural) the posts own the strings and the OrderedStrings...
r_std_hashmap/36349_84_0.txt
Code Link
r_std_hashmap/36349_78_0.txt
Answer selected by warriorstar-orion Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment
r_std_hashmap/writing-a-hashmap-to-struct-procedural-macro-in-rust8_17_0.txt
and adding a ` #[derive(FromHashmap)] ` to the struct's declaration. To check out the generated code, give rustc the arguments ` -Z unstable- options --pretty=expanded ` . This will expand all the macros in the source and output it to the console. Running ` cargo rustc -- -Z unstable-options --pretty=expanded > expand...
r_std_hashmap/enum.Type.html5_31_0.txt
source #### pub fn from_texpr ( t: Spanned < TypeExpr >, t_context: TypeContext , t_decs: &Dag< HashMap < String , Type , RandomState >, String , u32 > ) -> Result < Type , Spanned < Error >>
r_std_hashmap/enum.Type.html5_72_0.txt
#### fn into (self) -> U Calls ` U::from(self) ` .
r_std_hashmap/36349_56_0.txt
* # {{editor}}'s edit
r_std_hashmap/enum.Type.html5_81_0.txt
source § ### impl<T> ToString for T where T: Display \+ ? Sized ,
r_std_hashmap/614907_21_0.txt
Ok, so let's assume you need that ` Vec ` Now, where's that ` BTreeSet ` from? Whey are there ` &str ` in it? Are they ` &'static str ` or are they borrowing from somewhere else? That should very much inform the further choices. Also, forget about using ` str ` directly, you can't have a ` Vec<str> ` . Here is a vers...
r_std_hashmap/enum.Type.html5_82_0.txt
source § #### default fn to_string (&self) -> String
r_std_hashmap/36349_16_0.txt
Fund open source developers * The ReadME Project
r_std_hashmap/enum.Type.html5_7_0.txt
* from_enumdef * from_structdef * from_texpr * from_typedef * is_stable * sub_generic * well_formed ### Trait Implementations
r_std_hashmap/36349_37_0.txt
edited *
r_std_hashmap/enum.Type.html5_20_0.txt
### List( Box < Type , Global >) §
r_std_hashmap/36349_29_0.txt
* Notifications * Fork 665 * Star 11.1k * Code * Issues 184 * Pull requests 49 * Discussions * Actions * Projects 1 * Security * Insights
r_std_hashmap/614907_47_0.txt
agiuliano June 24, 2021, 8:37pm 15 Yeah I guess if I extract the read from file function it can work. My problem here is probably around the encapsulation.. What I want to do is that OrderedStrings can be created from the outside world only with a treeset (to enforce the strings are in order) but then inside the i...
r_std_hashmap/enum.Type.html5_73_0.txt
That is, this conversion is whatever the implementation of ` From <T> for U ` chooses to do. source §
r_std_hashmap/enum.Type.html5_12_0.txt
source · [ − ] pub enum Type { Show 16 variants Unit, Int, Float, String, Bool, Tuple(Vec<Box<Type, Global>, Global>), List(Box<Type, Global>), Function(Box<Type, Global>, Box<Type, Global>), Delay(Box<Type, Global>), Stab...
r_std_hashmap/36349_62_0.txt
struct BarWrapper { foo: Py<FooWrapper>, key: String, index: usize, } impl BarWrapper { fn value(&self, py: Python<'_>) -> &Bar { &self.foo.borrow(py).0[&self.key][self.index] } } (I have not compiled this, so it will most likely trigger some com...
r_std_hashmap/614907_14_0.txt
3 Likes agiuliano June 24, 2021, 1:09pm 5
r_std_hashmap/enum.Type.html5_45_0.txt
source § ### impl Display for Type
r_std_hashmap/614907_7_0.txt
1. ` index_by_symbol ` in the struct should probably be ` &str ` to get a reference, but then I get a suggestion to use lifetimes.. 2. in the new method I accept an ordered set of strings (where I don't take ownership) that makes things complex when I want to use plain ` str ` Cerber-Ursi June 24, 2021, 11:44am ...
r_std_hashmap/36349_34_0.txt
Answered by adamreichold Return to top Discussion options
r_std_hashmap/614907_4_0.txt
Now what I'm bugged with is ownership.. since the vector and hashmap contain the exact same string, I'd like to store them only once in memory. Do you have any suggestion on how to do it?
r_std_hashmap/enum.Type.html5_78_0.txt
#### fn clone_into (&self, target: &mut T ) Uses borrowed data to replace owned data, usually by cloning. Read more
r_std_hashmap/r_std_hashmap_2_2.txt
%0A++++\(%22Venus%22,+0.7\),%0A++++\(%22Earth%22,+1.0\),%0A++++\(%22Mars%22,+1.5\),%0A%5D\);%0A%7D&edition=2021) ` HashMap ` implements an ` Entry ` API , which allows for complex methods of getting, setting, updating and removing keys and their values: use std::collections::HashMap; // type ...
r_std_hashmap/614907_10_0.txt
This combination of requirements hints that you probably want to use ` Rc<str> ` (or ` Arc<str> ` , if you need ` Send ` / ` Sync ` ) - a smart pointer for shared immutable ownership. 5 Likes
r_std_hashmap/enum.Type.html5_3_0.txt
* Versions * * Platform * i686-unknown-linux-gnu * x86_64-apple-darwin * x86_64-pc-windows-msvc * x86_64-unknown-linux-gnu * Feature flags * Rust * About docs.rs * Privacy policy * Rust website * The Book * Standard Library API Reference * Rust by Exam...
r_std_hashmap/r_std_hashmap_8_0.txt
1.6.0 · [ source ](../../src/std/collections/hash/map.rs.html#618-620) #### pub fn drain (&mut self) -> [ Drain ](hash_map/struct.Drain.html "struct std::collections::hash_map::Drain") <'_, K, V> ⓘ Clears the map, returning all key-value pairs as an iterator. Keeps the allocated memory for reuse. If the retur...
r_std_hashmap/writing-a-hashmap-to-struct-procedural-macro-in-rust8_15_0.txt
#![recursion_limit = "128"] extern crate proc_macro; extern crate syn; #[macro_use] extern crate quote; use proc_macro::TokenStream; use syn::{Ident, VariantData}; #[proc_macro_derive(FromHashmap)] pub fn from_hashmap(input: TokenStream) -> TokenStream { ...
r_std_hashmap/614907_61_0.txt
Topic | | Replies | Views | Activity ---|---|---|---|--- Structs, closures, references and Strings, oh my! help | 6 | 1113 | January 12, 2023 A little confused about String vs str, ownership, and Copy trait help | 5 | 1342 | March 16, 2022 Rust how to convert vector of raw strings to str...
r_std_hashmap/enum.Type.html5_89_0.txt
#### type Error = <U as TryFrom <T>>:: Error The type returned in the event of a conversion error.
r_std_hashmap/enum.Type.html5_85_0.txt
#### type Error = Infallible The type returned in the event of a conversion error.
r_std_hashmap/614907_11_0.txt
KillTheMule June 24, 2021, 12:08pm 3 Why do you need the extra ` Vec<String> ` ? You can iterate over the keys of the ` HashMap ` just as well.
r_std_hashmap/36349_72_0.txt
* # {{title}}
r_std_hashmap/enum.Type.html5_29_0.txt
### Enum( HashMap < String , Option < Box < Type , Global >>, RandomState >) ## Implementations §
r_std_hashmap/614907_31_0.txt
In the scenario you described I think Option 1 makes much more sense. The shape of the code will be like let posts = get_stuff_from_db(); let ordered_strings = Odered_Strings::from_posts(&posts); // borrowing from the above ... //do stuff with ordered strings // ordered_strings...
r_std_hashmap/36349_32_0.txt
warriorstar-orion asked this question in Questions Sharing Rust structs in foreign libraries / Writing complex wrapper classes #3634
r_std_hashmap/r_std_hashmap_11_0.txt
1.57.0 · [ source ](../../src/std/collections/hash/map.rs.html#783-785) #### pub fn try_reserve (&mut self, additional: [ usize ](../primitive.usize.html) ) -> [ Result ](../result/enum.Result.html "enum std::result::Result") < [ () ](../primitive.unit.html) , [ TryReserveError ](struct.TryReserveError.html "stru...
r_std_hashmap/614907_54_0.txt
agiuliano June 27, 2021, 10:14am 18 agiuliano:
r_std_hashmap/writing-a-hashmap-to-struct-procedural-macro-in-rust8_18_0.txt
... impl FromHashmap<SimBrokerSettings> for SimBrokerSettings { fn from_hashmap(mut hm: ::std::collections::HashMap<String, String>) -> SimBrokerSettings { let mut settings = SimBrokerSettings::default(); match hm.entry(String::...
r_std_hashmap/36349_41_0.txt
Dec 8, 2023 \-
r_std_hashmap/36349_46_0.txt
My understanding is that by assigning a bar to a python local, we're crossing this boundary where the native Bar struct needs to be owned by both the ` vec ` inside the Foo's ` IndexMap ` , as well as by Python, because the BarWrapper could exist forever and be assigned to as many locals as we want. It seems like ` PyC...
r_std_hashmap/614907_48_0.txt
So pretty much when using the struct impl you can: * create from an ordered set * flush on a buffer ` self ` * read from a buffer to recreate the struct
r_std_hashmap/enum.Type.html5_14_0.txt
### Unit §
r_std_hashmap/36349_36_0.txt
Something went wrong. Quote reply
r_std_hashmap/enum.Type.html5_13_0.txt
## Variants § §
r_std_hashmap/r_std_hashmap_17_1.txt
#tymethod.from) (t: T) -> T Returns the argument unchanged. [ source ](../../src/core/convert/mod.rs.html#747-749) § ### impl<T, U> [ Into ](../convert/trait.Into.html "trait std::convert::Into") <U> for T where U: [ From ](../convert/trait.From.html "trait std::convert::From") <T>, [ source ](../../src/core/con...
r_std_hashmap/enum.Type.html5_53_0.txt
§ ### impl RefUnwindSafe for Type
r_std_hashmap/614907_8_0.txt
agiuliano: > since the vector and hashmap contain the exact same string, I'd like to > store them only once in memory.
r_std_hashmap/enum.Type.html5_87_0.txt
Performs the conversion. source §
r_std_hashmap/enum.Type.html5_22_0.txt
### Delay( Box < Type , Global >) §
r_std_hashmap/36349_3_0.txt
Automate any workflow * Packages
r_std_hashmap/36349_35_0.txt
* # {{title}}
r_std_hashmap/enum.Type.html5_80_0.txt
§ #### fn to_sample_ (self) -> U
r_std_hashmap/614907_49_0.txt
that's the reason why I wanted to have the read inside.. I've noticed that rust was bringing me to push stuff out, my only fear is that often it's just moving the problem one step away but it will still be there, so that's why I wonder whether I'm missing something (I'm sure I am) KillTheMule June 24, 2021, 8:58pm 1...
r_std_hashmap/36349_80_0.txt
Questions Labels
r_std_hashmap/r_std_hashmap_2_1.txt
res of Huckleberry Finn".to_string(), "My favorite book.".to_string(), ); book_reviews.insert( "Grimms' Fairy Tales".to_string(), "Masterpiece.".to_string(), ); book_reviews.insert( "Pride and Prejudice".to_string(), "Very enjoyable.".to_string(), ); book_...
r_std_hashmap/enum.Type.html5_57_0.txt
§ ### impl UnwindSafe for Type
r_std_hashmap/enum.Type.html5_33_0.txt
source #### pub fn from_structdef ( id: String , params: Vec < String , Global >, fields: Vec <( String , Spanned < TypeExpr >), Global >, t_decs: &Dag< HashMap < String , Type , RandomState >, String , u32 > ) -> Result < Type , Spanned < Error >>
r_std_hashmap/writing-a-hashmap-to-struct-procedural-macro-in-rust8_6_0.txt
Then I read about Rust's new procedural macros. They provide a way to generate new code at compile-time, making it possible to do things like automatically derive the ` Serialize ` and ` Deserialize ` in serde . I decided to create a ` FromHashmap ` trait backed by a procedural macro which would allow me to automatic...
r_std_hashmap/enum.Type.html5_6_0.txt
* Bool * Delay * Enum * Fix * FixVar * Float * Function * Generic * GenericVar * Int * List * Stable * String * Struct * Tuple * Unit ### Methods
r_std_hashmap/36349_91_0.txt
👍 1 reacted with thumbs up emoji 👎 1 reacted with thumbs down emoji 😄 1 reacted with laugh emoji 🎉 1 reacted with hooray emoji 😕 1 reacted with confused emoji ❤️ 1 reacted with heart emoji 🚀 1 reacted with rocket emoji 👀 1 reacted with eyes emoji ## Footer
r_std_hashmap/enum.Type.html5_55_0.txt
§ ### impl Sync for Type
r_std_hashmap/r_std_hashmap_13_0.txt
1.40.0 · [ source ](../../src/std/collections/hash/map.rs.html#903-909) #### pub fn get_key_value <Q>(&self, k: [ &Q ](../primitive.reference.html) ) -> [ Option ](../option/enum.Option.html "enum std::option::Option") <( [ &K ](../primitive.reference.html) , [ &V ](../primitive.reference.html) )> where K: [ Bor...
r_std_hashmap/enum.Type.html5_30_0.txt
source § ### impl Type
r_std_hashmap/36349_74_0.txt
#### adamreichold Dec 19, 2023
r_std_hashmap/36349_4_0.txt
Host and manage packages * Security
r_std_hashmap/enum.Type.html5_38_0.txt
## Trait Implementations § source §
r_std_hashmap/enum.Type.html5_83_0.txt
Converts the given value to a ` String ` . Read more source §
r_std_hashmap/enum.Type.html5_42_0.txt
Performs copy-assignment from ` source ` . Read more source §
r_std_hashmap/r_std_hashmap_10_0.txt
1.9.0 · [ source ](../../src/std/collections/hash/map.rs.html#728-730) #### pub fn hasher (&self) -> [ &S ](../primitive.reference.html) Returns a reference to the map’s [ ` BuildHasher ` ](../hash/trait.BuildHasher.html "trait std::hash::BuildHasher") . ##### § Examples use std::collections...
r_std_hashmap/writing-a-hashmap-to-struct-procedural-macro-in-rust8_14_0.txt
For each of the elements of the vectors interpolated inside the repetition block, the entire inner part of the repetition block is inserted into the code. In the test, the ` ,* ` at the end tells Quote to insert a comma as a separator, very similar to ` .join() ` . So now we have code that parses the input struct into...
r_std_hashmap/36349_76_0.txt
Yes, if the ` Bar ` are created in standalone manner, the above seems like a good approach. Alternatively, you could limit creation of ` Bar ` from Python to methods on ` FooWrapper ` which would take care of creation and insertion as a single operation (from Python's point of view). --- Beta Was this translati...
r_std_hashmap/enum.Type.html5_15_0.txt
### Int §
r_std_hashmap/enum.Type.html5_32_0.txt
source #### pub fn from_typedef ( id: String , params: Vec < String , Global >, t: Spanned < TypeExpr >, t_decs: &Dag< HashMap < String , Type , RandomState >, String , u32 >, path: & Vec < String , Global > ) -> Result < Type , Spanned < Error >>
r_std_hashmap/writing-a-hashmap-to-struct-procedural-macro-in-rust8_8_0.txt
It requires that structs that implement it also implement the ` std::default::Default ` trait so that defaults can be used if the provided HashMap doesn't contain all the necessary values. Since procedural macros must reside in their own special crate, I created a new one inside my crate's root directory (at the same ...
r_std_hashmap/36349_61_0.txt
> You can see the "foreign" structs are: Those structures do not support shared ownership (not even in Rust) and hence you cannot use that on language level. You would would have to introduce something like ` Arc ` or our own ` Py ` smart pointer into the mix so that you would store e.g. ` HashMap<String, Vec<Arc<Ba...
r_std_hashmap/enum.Type.html5_66_0.txt
source § ### impl<T> From <T> for T
r_std_hashmap/614907_56_0.txt
let hello_var = "hello" ` hello_var ` is a reference to a String that is held on the stack (because it's not a String but a str).. should I be thinking to have a str instead?
r_std_hashmap/36349_57_0.txt
{{actor}} deleted this content . # {{editor}}'s edit
r_std_hashmap/614907_16_0.txt
> Why do you need the extra ` Vec<String> ` ? You can iterate over the keys of > the ` HashMap ` just as well. I know I can but that vector is ordered.. so I need to access the strings by index, and the index by strings. You can see that as if I have 2 maps:
r_std_hashmap/36349_26_0.txt
Cancel Create saved search Sign in
r_std_hashmap/36349_81_0.txt
None yet 2 participants
r_std_hashmap/614907_2_0.txt
Hi all, as I'm playing with Rust, I'm facing some of the typical (I hope) problems with borrowing especially when handling strings. Here's my problem: say I want to have a struct of this shape
r_std_hashmap/36349_19_0.txt
Search or jump to... # Search code, repositories, users, issues, pull requests...
r_std_hashmap/enum.Type.html5_9_0.txt
* RefUnwindSafe * Send * Sync * Unpin * UnwindSafe ### Blanket Implementations
r_std_hashmap/614907_57_0.txt
Cerber-Ursi June 27, 2021, 10:21am 19 agiuliano:
r_std_hashmap/36349_48_0.txt
1 You must be logged in to vote All reactions
r_std_hashmap/writing-a-hashmap-to-struct-procedural-macro-in-rust8_16_0.txt
Back in the original code, using the procedural macro is as simple as adding a ` from_hashmap = { path = "from_hashmap" } ` line to the crate's Cargo.toml, importing it with #[macro_use] extern crate from_hashmap;
r_std_hashmap/enum.Type.html5_52_0.txt
### impl StructuralPartialEq for Type ## Auto Trait Implementations §
r_std_hashmap/r_std_hashmap_3_0.txt
1.7.0 (const: [ unstable ](https://github.com/rust-lang/rust/issues/102575 "Tracking issue for const_collections_with_hasher") ) · [ source ](../../src/std/collections/hash/map.rs.html#283-285) #### pub fn with_hasher (hash_builder: S) -> [ HashMap ](hash_map/struct.HashMap.html "struct std::collections::hash_map...
r_std_hashmap/36349_47_0.txt
#[pyclass] struct FooWrapper { foo: foreign::Foo, bars: PyDict<PyList<BarWrapper>>, } #[pymethods] impl FooWrapper { #[new] fn new() -> Self { // Remove all the elements from foo.mapping so they can be owned by the wrapper/pylist/pydict ...
r_std_hashmap/36349_64_0.txt
All reactions 2 replies
r_std_hashmap/614907_25_0.txt
Option 1 has the advantage that is clear that the main struct owns all the data, and uses the other structs to just create a projected view. However to do that I have to use lifetimes (maybe I'm too newbie and that's what you end up doing most of the cases) Option 2 instead is nice in a way because I can destruct my m...
r_std_hashmap/writing-a-hashmap-to-struct-procedural-macro-in-rust8_10_0.txt
The next step is to get get a list of all the fields of the provided struct. These are represented as Idents and can be found deep within the AST. This code unpacks the AST, makes sure that what we're given is actually a standard, non-tuple struct, and puts all of the field names into a vector: // crea...
r_std_hashmap/614907_53_0.txt
fn main() { let point = Point { x: 1, y: 2 }; let serialized = serde_json::to_string(&point).unwrap(); println!("serialized = {}", serialized); let deserialized: Point = serde_json::from_str(&serialized).unwrap(); println!("deserialized = {:?}", deserializ...
r_std_hashmap/36349_68_0.txt
warriorstar-orion Dec 19, 2023 Author
r_std_hashmap/614907_51_0.txt
Yeah in Rust you want to push ownership of things high up in the scope tree. If you don't want to do that explicitely, you might want to think about a top- level wrapper type that users interact with that can own things. As an entry- point, the user would construct the top-level type with the necessary data (database c...
r_std_hashmap/enum.Type.html5_56_0.txt
§ ### impl Unpin for Type