Skip to content

Instantly share code, notes, and snippets.

@shepmaster
Created June 2, 2017 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shepmaster/88f42073bdd3d5159346e83d458c0026 to your computer and use it in GitHub Desktop.
Save shepmaster/88f42073bdd3d5159346e83d458c0026 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:re="http://purl.org/atompub/rank/1.0">
<title type="text">Active questions tagged rust - Stack Overflow</title>
<link rel="self" href="https://stackoverflow.com/feeds/tag?tagnames=rust" type="application/atom+xml" />
<link rel="alternate" href="https://stackoverflow.com/questions/tagged/?tagnames=rust&amp;sort=active" type="text/html" />
<subtitle>most recent 30 from stackoverflow.com</subtitle>
<updated>2017-06-02T13:24:18Z</updated>
<id>https://stackoverflow.com/feeds/tag?tagnames=rust</id>
<creativeCommons:license>http://www.creativecommons.org/licenses/by-sa/3.0/rdf</creativeCommons:license>
<entry>
<id>https://stackoverflow.com/q/44325802</id>
<re:rank scheme="https://stackoverflow.com">-1</re:rank>
<title type="text">Deal with large numbers</title>
<category scheme="https://stackoverflow.com/tags" term="rust" />
<author>
<name>Darkaird</name>
<uri>https://stackoverflow.com/users/7546477</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44325802/deal-with-large-numbers" />
<published>2017-06-02T09:45:52Z</published>
<updated>2017-06-02T11:01:44Z</updated>
<summary type="html">
&lt;p&gt;When I try to do 10 power 100, I get &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;thread &#39;main&#39; panicked at &#39;attempt to multiply with overflow&#39;, shorter.rs:33
note: Run with `RUST_BACKTRACE=1` for a backtrace.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which is normal because 10^100 is greater than 2^64 (and even 2^128). &lt;/p&gt;
&lt;p&gt;So my question is : how to deal with reals larges numbers which don&#39;t fit in 128 bits in Rust without loss ?&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44326928</id>
<re:rank scheme="https://stackoverflow.com">2</re:rank>
<title type="text">Use Index trait with HashMap in Rust</title>
<category scheme="https://stackoverflow.com/tags" term="hashmap" />
<category scheme="https://stackoverflow.com/tags" term="rust" />
<author>
<name>mike</name>
<uri>https://stackoverflow.com/users/1809463</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44326928/use-index-trait-with-hashmap-in-rust" />
<published>2017-06-02T10:39:15Z</published>
<updated>2017-06-02T10:58:42Z</updated>
<summary type="html">
&lt;p&gt;In order to test the &lt;a href=&quot;https://doc.rust-lang.org/std/ops/trait.Index.html&quot; rel=&quot;nofollow noreferrer&quot;&gt;&lt;code&gt;Index&lt;/code&gt;&lt;/a&gt; trait, I coded a histogram. &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;use std::collections::HashMap;
fn main() {
let mut histogram: HashMap&amp;lt;char, u32&amp;gt; = HashMap::new();
let chars: Vec&amp;lt;_&amp;gt; = &quot;Lorem ipsum dolor sit amet&quot;
.to_lowercase()
.chars()
.collect();
for c in chars {
histogram[c] += 1;
}
println!(&quot;{:?}&quot;, histogram);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;sup&gt;Test code &lt;a href=&quot;https://play.rust-lang.org/?code=use%20std%3A%3Acollections%3A%3AHashMap%3B%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20mut%20histogram%3A%20HashMap%3Cchar%2C%20u32%3E%20%3D%20HashMap%3A%3Anew()%3B%0A%20%20%20%20let%20chars%3A%20Vec%3C_%3E%20%3D%20%22Lorem%20ipsum%20dolor%20sit%20amet%22%0A%20%20%20%20%20%20%20%20.to_lowercase()%0A%20%20%20%20%20%20%20%20.chars()%0A%20%20%20%20%20%20%20%20.collect()%3B%0A%0A%20%20%20%20for%20c%20in%20chars%20%7B%0A%20%20%20%20%20%20%20%20histogram%5Bc%5D%20%2B%3D%201%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20println!(%22%7B%3A%3F%7D%22%2C%20histogram)%3B%0A%7D%0A&amp;amp;version=stable&amp;amp;backtrace=0&quot; rel=&quot;nofollow noreferrer&quot;&gt;here&lt;/a&gt;.&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;But I get following type error &lt;code&gt;expected &amp;amp;char, found char&lt;/code&gt;. If I use &lt;code&gt;histogram[&amp;amp;c] += 1;&lt;/code&gt; instead, I get &lt;code&gt;cannot borrow as mutable&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;What am I doing wrong? How can I fix this example?&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44324811</id>
<re:rank scheme="https://stackoverflow.com">-2</re:rank>
<title type="text">Rust chrono::DateTime subtraction not implemented</title>
<category scheme="https://stackoverflow.com/tags" term="datetime" />
<category scheme="https://stackoverflow.com/tags" term="rust" />
<author>
<name>LizzAlice</name>
<uri>https://stackoverflow.com/users/7253302</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44324811/rust-chronodatetime-subtraction-not-implemented" />
<published>2017-06-02T08:59:35Z</published>
<updated>2017-06-02T08:59:35Z</updated>
<summary type="html">
&lt;p&gt;I want to subtract two chrono::DateTime in Rust. According to other posts, the sub trait is implemented for DateTime, but I still get the error &lt;code&gt;binary operation `-` cannot be applied to type `std::vec::Vec&amp;lt;chrono::DateTime&amp;lt;chrono::UTC&amp;gt;&amp;gt;&lt;/code&gt;. Did I maybe use it in the wrong way?&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/31374051</id>
<re:rank scheme="https://stackoverflow.com">5</re:rank>
<title type="text">Why does Iterator::take_while take ownership of the iterator?</title>
<category scheme="https://stackoverflow.com/tags" term="rust" />
<author>
<name>Alex Ozdemir</name>
<uri>https://stackoverflow.com/users/4917890</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/31374051/why-does-iteratortake-while-take-ownership-of-the-iterator" />
<published>2015-07-13T00:48:50Z</published>
<updated>2017-06-02T05:03:45Z</updated>
<summary type="html">
&lt;p&gt;I find it odd that Iterator::take_while takes ownership of the iterator. It seems like a useful feature to be able to take the first &lt;code&gt;x&lt;/code&gt; elements which satisfy some function but still leave the rest of the elements available in the original iterator.&lt;/p&gt;
&lt;p&gt;I understand that this is incompatible with a lazy implementation of take_while, but still feels useful. Was this just judged not useful enough to include in &lt;code&gt;std&lt;/code&gt;, or is there some other problem I&#39;m not seeing?&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44231020</id>
<re:rank scheme="https://stackoverflow.com">3</re:rank>
<title type="text">How can deserialization of polymorphic trait objects be added in Rust if at all?</title>
<category scheme="https://stackoverflow.com/tags" term="serialization" />
<category scheme="https://stackoverflow.com/tags" term="rust" />
<category scheme="https://stackoverflow.com/tags" term="serde" />
<author>
<name>Dmitry Gordon</name>
<uri>https://stackoverflow.com/users/5902079</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44231020/how-can-deserialization-of-polymorphic-trait-objects-be-added-in-rust-if-at-all" />
<published>2017-05-28T19:07:26Z</published>
<updated>2017-06-01T23:18:29Z</updated>
<summary type="html">
&lt;p&gt;I&#39;m trying to solve the problem of serializing and deserializing &lt;code&gt;Box&amp;lt;SomeTrait&amp;gt;&lt;/code&gt;. I know that in the case of a closed type hierarchy, the recommended way is to use an enum and there are no issues with their serialization, but in my case using enums is an inappropriate solution.&lt;/p&gt;
&lt;p&gt;At first I tried to use Serde as it is the de-facto Rust serialization mechanism. Serde is capable of serializing &lt;code&gt;Box&amp;lt;X&amp;gt;&lt;/code&gt; but not in the case when &lt;code&gt;X&lt;/code&gt; is a trait. The &lt;a href=&quot;https://docs.serde.rs/serde/trait.Serialize.html&quot; rel=&quot;nofollow noreferrer&quot;&gt;&lt;code&gt;Serialize&lt;/code&gt;&lt;/a&gt; trait can’t be implemented for trait objects because it has generic methods. This particular issue can be solved by using &lt;a href=&quot;https://github.com/dtolnay/erased-serde&quot; rel=&quot;nofollow noreferrer&quot;&gt;erased-serde&lt;/a&gt; so serialization of &lt;code&gt;Box&amp;lt;SomeTrait&amp;gt;&lt;/code&gt; can work.&lt;/p&gt;
&lt;p&gt;The main problem is deserialization. To deserialize polymorphic type you need to have some type marker in serialized data. This marker should be deserialized first and after that used to dynamically get the function that will return &lt;code&gt;Box&amp;lt;SomeTrait&amp;gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;std::any::TypeId&lt;/code&gt; could be used as a marker type, but the main problem is how to dynamically get the deserialization function. I do not consider the option of registering a function for each polymorphic type that should be called manually during application initialization.&lt;/p&gt;
&lt;p&gt;I know two possible ways to do it:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Languages that have runtime reflection like C# can use it to get
deserialization method. &lt;/li&gt;
&lt;li&gt;In C++, the cereal library uses magic of static objects to register deserializer in a static map at the library initialization time.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;But neither of these options is available in Rust. How can deserialization of polymorphic objects be added in Rust if at all? &lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44318158</id>
<re:rank scheme="https://stackoverflow.com">-2</re:rank>
<title type="text">Using a String/&amp;str as a type to downcast an Any reference</title>
<category scheme="https://stackoverflow.com/tags" term="rust" />
<author>
<name>Boris</name>
<uri>https://stackoverflow.com/users/5223795</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44318158/using-a-string-str-as-a-type-to-downcast-an-any-reference" />
<published>2017-06-01T22:37:08Z</published>
<updated>2017-06-01T23:12:30Z</updated>
<summary type="html">
&lt;p&gt;I&#39;m trying to create a map where a key is a string and each value can be a different type. Storing values works fine using &lt;code&gt;Box&amp;lt;Any&amp;gt;&lt;/code&gt;, but I can&#39;t find a way to retrieve a typed value. I experimented around with &lt;code&gt;Any::downcast_ref()&lt;/code&gt; using the type stored along with the value in string format but I couldn&#39;t get it to work (see sample code below). &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Is there a way to use a type stored as a &lt;code&gt;String&lt;/code&gt; to set a variable&#39;s type (using a &lt;code&gt;:&lt;/code&gt; or &lt;code&gt;::&amp;lt;&amp;gt;&lt;/code&gt;) ?&lt;/li&gt;
&lt;li&gt;If not, is there a way to create a map of &quot;anything&quot; and get back a value (ideally not a reference)?&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code&gt;#![feature(core_intrinsics)]
use std::any::Any;
use std::collections::BTreeMap;
use std::intrinsics::type_name;
#[derive(Debug)]
struct TypedAny {
value: Box&amp;lt;Any&amp;gt;,
type_name: String,
}
impl TypedAny {
fn new&amp;lt;T: Any + Sized&amp;gt;(val: T) -&amp;gt; TypedAny {
let type_name = unsafe { type_name::&amp;lt;T&amp;gt;() };
TypedAny { value: Box::new(val), type_name: type_name.to_string() }
}
}
fn main() {
let mut map : BTreeMap&amp;lt;&amp;amp;str, TypedAny&amp;gt; = BTreeMap::new();
map.insert(&quot;key 1&quot;, TypedAny::new(1 as u64));
map.insert(&quot;key 2&quot;, TypedAny::new(&quot;some string&quot;.to_string()));
println!(&quot;map = {:?}&quot;, map);
let val = map.get(&quot;key 1&quot;).unwrap();
println!(&quot;val = {:?}&quot;, val);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It compiles and prints this when run&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;map = {&quot;key 1&quot;: TypedAny { value: Any, type_name: &quot;u64&quot; }, &quot;key 2&quot;: TypedAny { value: Any, type_name: &quot;std::string::String&quot; }}
val = TypedAny { value: Any, type_name: &quot;u64&quot; }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I was trying to do a macro to call &lt;code&gt;Any::downcast_ref()&lt;/code&gt; but I couldn&#39;t get it to compile.&lt;/p&gt;
&lt;p&gt;I then tried to add the following macro code (above the TypedAny definition) ...&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;macro_rules! __downcast_value {
($value:expr, $name:ty) =&amp;gt; {
{
let value : $name = $value.downcast_ref();
value
}
}
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;... and a new method to &lt;code&gt;TypedAny&lt;/code&gt; ...&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;fn downcast_ref&amp;lt;T: Any + Sized&amp;gt;(&amp;amp;self) -&amp;gt; &amp;amp;T {
let name = self.type_name;
__downcast_value!(self.value, name)
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;... but it leads to this compilation error&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;error[E0573]: expected type, found local variable `name`
--&amp;gt; src/main.rs:30:39
|
30 | __downcast_value!(self.value, name)
| ^^^^ not a type
&lt;/code&gt;&lt;/pre&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44249803</id>
<re:rank scheme="https://stackoverflow.com">0</re:rank>
<title type="text">How do we convert a Rust string to a gtk::type::String?</title>
<category scheme="https://stackoverflow.com/tags" term="rust" />
<category scheme="https://stackoverflow.com/tags" term="gtk-rs" />
<author>
<name>Geob-o-matic</name>
<uri>https://stackoverflow.com/users/600351</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44249803/how-do-we-convert-a-rust-string-to-a-gtktypestring" />
<published>2017-05-29T20:16:07Z</published>
<updated>2017-06-01T22:01:02Z</updated>
<summary type="html">
&lt;p&gt;I&#39;m trying to create a &lt;code&gt;ComboBox&lt;/code&gt;, especially the model for it:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;let type_in_col = &amp;amp;[gtk::Type::String];
let list_model = ListStore::new(type_in_col);
list_model.insert_with_values(None, &amp;amp;[0], &amp;amp;[&quot;&quot;]);
list_model.insert_with_values(None, &amp;amp;[0], &amp;amp;[&quot;h&quot;]);
list_model.insert_with_values(None, &amp;amp;[0], &amp;amp;[&quot;H&quot;]);
list_model.insert_with_values(None, &amp;amp;[0], &amp;amp;[&quot;W&quot;]);
list_model.insert_with_values(None, &amp;amp;[0], &amp;amp;[&quot;S&quot;]);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This piece of code gave me this error:&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied
--&amp;gt; src\widgets\daywidget.rs:36:1
|
36 | #[widget]
| ^^^^^^^^^ `str` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `str`
= note: required for the cast to the object type `gtk::ToValue`
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(the error is not very precise because I&#39;m using Relm)&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44302604</id>
<re:rank scheme="https://stackoverflow.com">2</re:rank>
<title type="text">How to deserialize a subfield of a struct from the original struct&#39;s JSON with Serde?</title>
<category scheme="https://stackoverflow.com/tags" term="json" />
<category scheme="https://stackoverflow.com/tags" term="rust" />
<category scheme="https://stackoverflow.com/tags" term="serde" />
<author>
<name>Victor Polevoy</name>
<uri>https://stackoverflow.com/users/1750757</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44302604/how-to-deserialize-a-subfield-of-a-struct-from-the-original-structs-json-with-s" />
<published>2017-06-01T08:41:55Z</published>
<updated>2017-06-01T21:18:05Z</updated>
<summary type="html">
&lt;p&gt;I want to have the &lt;code&gt;Test::team_size&lt;/code&gt; attribute be deserialized from the data of &lt;code&gt;Test&lt;/code&gt; object itself:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#[derive(Debug, Serialize, Deserialize)]
struct TeamSize {
pub min: i64,
pub max: i64,
}
#[derive(Debug, Serialize, Deserialize)]
struct Test {
pub i: i64,
pub team_size: TeamSize,
}
fn main() {
let t: Test = serde_json::from_str(r#&quot;{&quot;i&quot;: -2, &quot;min&quot;: 2, &quot;max&quot;: 5}&quot;#).unwrap();
assert_eq!(t.i, -2);
assert_eq!(t.team_size.min, 2);
assert_eq!(t.team_size.max, 5);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This code does not compile and I don&#39;t know how to make Serde do what I want. Is there a way to deserialize &lt;code&gt;team_size&lt;/code&gt; in this example from the JSON of the original structure where it is a subfield?&lt;/p&gt;
&lt;p&gt;It seems that I want something &lt;strong&gt;like&lt;/strong&gt; &lt;a href=&quot;https://serde.rs/enum-representations.html&quot; rel=&quot;nofollow noreferrer&quot;&gt;&lt;code&gt;#[serde(untagged)]&lt;/code&gt;&lt;/a&gt; but for a struct and for a field and not the whole struct.&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44307780</id>
<re:rank scheme="https://stackoverflow.com">2</re:rank>
<title type="text">Vec&lt;T&gt; reference from trait and T lifetime</title>
<category scheme="https://stackoverflow.com/tags" term="rust" />
<author>
<name>user1244932</name>
<uri>https://stackoverflow.com/users/1244932</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44307780/vect-reference-from-trait-and-t-lifetime" />
<published>2017-06-01T12:38:20Z</published>
<updated>2017-06-01T20:56:20Z</updated>
<summary type="html">
&lt;p&gt;I want to work with several &lt;code&gt;Vec&lt;/code&gt; at once. These vectors can be specialized with different types. I created a trait:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;trait Column {
//fn insert
//fn remove
}
impl&amp;lt;T&amp;gt; Column for Vec&amp;lt;T&amp;gt; // ...
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I can convert &lt;code&gt;Vec&amp;lt;T1&amp;gt;&lt;/code&gt; and &lt;code&gt;Vec&amp;lt;T2&amp;gt;&lt;/code&gt; to &lt;code&gt;Box&amp;lt;Column&amp;gt;&lt;/code&gt;, but I also need to convert &lt;code&gt;Box&amp;lt;Column&amp;gt;&lt;/code&gt; back to &lt;code&gt;Vec&lt;/code&gt;. As suggested in &lt;a href=&quot;https://stackoverflow.com/questions/33687447/how-to-get-a-struct-reference-from-a-boxed-trait&quot;&gt;How to get a struct reference from a boxed trait?&lt;/a&gt;, I wrote:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;use std::any::Any;
trait Column {
fn as_any(&amp;amp;self) -&amp;gt; &amp;amp;Any;
}
impl&amp;lt;T&amp;gt; Column for Vec&amp;lt;T&amp;gt;
where T: Default
{
fn as_any(&amp;amp;self) -&amp;gt; &amp;amp;Any {
self //ERROR!
}
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;but this code generates an error:&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;error[E0310]: the parameter type `T` may not live long enough
--&amp;gt; src/main.rs:11:9
|
11 | self //ERROR!
| ^^^^
|
= help: consider adding an explicit lifetime bound `T: &#39;static`...
note: ...so that the type `std::vec::Vec&amp;lt;T&amp;gt;` will meet its required lifetime bounds
--&amp;gt; src/main.rs:11:9
|
11 | self //ERROR!
| ^^^^
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;How can I fix this issue?&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44314403</id>
<re:rank scheme="https://stackoverflow.com">1</re:rank>
<title type="text">Define two traits such that a function must exist for the cartesian product of the two sets of types that implement it</title>
<category scheme="https://stackoverflow.com/tags" term="rust" />
<category scheme="https://stackoverflow.com/tags" term="traits" />
<category scheme="https://stackoverflow.com/tags" term="multiple-dispatch" />
<author>
<name>user8099375</name>
<uri>https://stackoverflow.com/users/8099375</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44314403/define-two-traits-such-that-a-function-must-exist-for-the-cartesian-product-of-t" />
<published>2017-06-01T18:12:01Z</published>
<updated>2017-06-01T18:12:01Z</updated>
<summary type="html">
&lt;p&gt;I would like to create two traits, &lt;code&gt;SaveSource&lt;/code&gt; and &lt;code&gt;SaveDestination&lt;/code&gt;, such that when some types implement these traits, the function:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;fn save(a, b)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;must be implemented for all &lt;code&gt;a : SaveSource&lt;/code&gt; and &lt;code&gt;b : SaveDestination&lt;/code&gt; (and when a new type is added to either &lt;code&gt;SaveSource&lt;/code&gt; or &lt;code&gt;SaveDestination&lt;/code&gt;, it must implement the &lt;code&gt;save&lt;/code&gt; function for all existing &lt;code&gt;SaveDestination&lt;/code&gt;s or &lt;code&gt;SaveSource&lt;/code&gt;s.&lt;/p&gt;
&lt;p&gt;Is something like this even possible? If not, is there anything I can use instead to get the same result?&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44313906</id>
<re:rank scheme="https://stackoverflow.com">1</re:rank>
<title type="text">How to get tokio-io&#39;s async_read for a File handle</title>
<category scheme="https://stackoverflow.com/tags" term="file" />
<category scheme="https://stackoverflow.com/tags" term="asynchronous" />
<category scheme="https://stackoverflow.com/tags" term="rust" />
<category scheme="https://stackoverflow.com/tags" term="streaming" />
<category scheme="https://stackoverflow.com/tags" term="rust-tokio" />
<author>
<name>xrl</name>
<uri>https://stackoverflow.com/users/311358</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44313906/how-to-get-tokio-ios-async-read-for-a-file-handle" />
<published>2017-06-01T17:39:26Z</published>
<updated>2017-06-01T17:57:02Z</updated>
<summary type="html">
&lt;p&gt;I want to stream lines out of a file handle and I do not know how to satisfy the trait bound that a &lt;code&gt;File&lt;/code&gt; has &lt;code&gt;async_read&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;use std::fs::File;
use std::io::{ BufReader, BufRead };
use tokio_core::reactor::Handle;
use tokio_io::io::lines;
use tokio_io::AsyncRead;
struct DockerLog {
path: String
}
impl DockerLog {
pub fn new(path: String) -&amp;gt; DockerLog {
DockerLog {
path: path
}
}
pub fn read_lines(&amp;amp;self, handle: &amp;amp;Handle) {
let file : File = File::open(&amp;amp;self.path[..]).unwrap();
let l = lines(BufReader::new(file));
}
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The error:&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;error[E0277]: the trait bound `std::fs::File: tokio_io::AsyncRead` is not satisfied
--&amp;gt; src/container/docker_logs.rs:20:17
|
20 | let l = lines(BufReader::new(file));
| ^^^^^ the trait `tokio_io::AsyncRead` is not implemented for `std::fs::File`
|
= note: required because of the requirements on the impl of `tokio_io::AsyncRead` for `std::io::BufReader&amp;lt;std::fs::File&amp;gt;`
= note: required by `tokio_io::io::lines`
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Looking over the &lt;a href=&quot;https://docs.rs/tokio-io/0.1.2/tokio_io/trait.AsyncRead.html&quot; rel=&quot;nofollow noreferrer&quot;&gt;&lt;code&gt;AsyncRead&lt;/code&gt;&lt;/a&gt;, it seems like a &lt;code&gt;File&lt;/code&gt; doesn&#39;t mark that it has non-blocking reads. Do I need to downgrade the &lt;code&gt;File&lt;/code&gt; to a &lt;code&gt;raw_fd&lt;/code&gt; and then do something there?&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44246528</id>
<re:rank scheme="https://stackoverflow.com">7</re:rank>
<title type="text">Why can&#39;t the compiler parse &quot;a as u32 &lt; b&quot; or similar?</title>
<category scheme="https://stackoverflow.com/tags" term="rust" />
<category scheme="https://stackoverflow.com/tags" term="syntax-error" />
<author>
<name>E_net4</name>
<uri>https://stackoverflow.com/users/1233251</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44246528/why-cant-the-compiler-parse-a-as-u32-b-or-similar" />
<published>2017-05-29T16:01:03Z</published>
<updated>2017-06-01T17:08:53Z</updated>
<summary type="html">
&lt;p&gt;The following code appears to be trivial and unambiguous (&lt;a href=&quot;http://play.integer32.com/?gist=3c2b3f0a0d51d5c06eaf9f2320983ffd&amp;amp;version=undefined&quot; rel=&quot;noreferrer&quot;&gt;Playground&lt;/a&gt;):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;let a: u16 = 5;
let b: u32 = 10;
let c = a as u32 &amp;lt; b;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Yet the latest compiler (as of 2017-05-30) fails with a syntax error:&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;error: expected one of `!`, `(`, `+`, `,`, `::`, `&amp;lt;`, or `&amp;gt;`, found `;`
--&amp;gt; src/main.rs:6:25
|
6 | let c = a as u32 &amp;lt; b;
|
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What is wrong with the compiler?&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44312652</id>
<re:rank scheme="https://stackoverflow.com">1</re:rank>
<title type="text">Why is `&amp;mut &amp;foo` valid but `&amp;mut a_ref_to_foo` is invalid?</title>
<category scheme="https://stackoverflow.com/tags" term="rust" />
<author>
<name>colinfang</name>
<uri>https://stackoverflow.com/users/691867</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44312652/why-is-mut-foo-valid-but-mut-a-ref-to-foo-is-invalid" />
<published>2017-06-01T16:21:11Z</published>
<updated>2017-06-01T17:00:36Z</updated>
<summary type="html">
&lt;p&gt;In the following example, &lt;code&gt;t1&lt;/code&gt; compiles but &lt;code&gt;t2&lt;/code&gt; does not. &lt;/p&gt;
&lt;p&gt;Is there anything special about &lt;code&gt;&amp;amp;mut &amp;amp;stream&lt;/code&gt;? I don&#39;t think &lt;code&gt;Deref&lt;/code&gt; kicks in.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;use std::net::TcpStream;
fn t1() {
let stream = TcpStream::connect(&quot;127.0.0.1&quot;).unwrap();
let a = &amp;amp;mut &amp;amp;stream;
}
fn t2(stream: &amp;amp;TcpStream) {
let a = &amp;amp;mut stream;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href=&quot;https://play.rust-lang.org/?gist=0757b5f5810ff917b93a45924db9f00e&amp;amp;version=stable&amp;amp;backtrace=0&quot; rel=&quot;nofollow noreferrer&quot;&gt;Playground&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;9 | fn t2(stream: &amp;amp;TcpStream) {
| ------ use `mut stream` here to make mutable
10 | let a = &amp;amp;mut stream;
| ^^^^^^ cannot borrow mutably
&lt;/code&gt;&lt;/pre&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/32429369</id>
<re:rank scheme="https://stackoverflow.com">11</re:rank>
<title type="text">How to opt out of running a doc test?</title>
<category scheme="https://stackoverflow.com/tags" term="rust" />
<category scheme="https://stackoverflow.com/tags" term="rust-cargo" />
<author>
<name>Craig M. Brandenburg</name>
<uri>https://stackoverflow.com/users/1094609</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/32429369/how-to-opt-out-of-running-a-doc-test" />
<published>2015-09-06T23:42:07Z</published>
<updated>2017-06-01T14:59:31Z</updated>
<summary type="html">
&lt;p&gt;I&#39;m writing a Rust library and I want to provide examples in my documentation that&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;compile as part of running &lt;code&gt;cargo test&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;do &lt;em&gt;not&lt;/em&gt; run. &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Is this possible?&lt;/p&gt;
&lt;p&gt;I&#39;m writing a database client library, and the examples make use of a hypothetical, non-existing database server. As such, the examples always fail when run, but it&#39;s important that the examples be valid syntactically. Hence my requirements above.&lt;/p&gt;
&lt;p&gt;If there&#39;s no way to do what I want, then how does one opt out of having &lt;code&gt;cargo test&lt;/code&gt; run a specific doc test? I.e., have &lt;code&gt;cargo run&lt;/code&gt; compile-and-run some doc tests but completely ignore some others?&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/43788943</id>
<re:rank scheme="https://stackoverflow.com">0</re:rank>
<title type="text">Send SIGINT to a process by sending ctrl-c to stdin</title>
<category scheme="https://stackoverflow.com/tags" term="process" />
<category scheme="https://stackoverflow.com/tags" term="rust" />
<category scheme="https://stackoverflow.com/tags" term="signals" />
<category scheme="https://stackoverflow.com/tags" term="pty" />
<author>
<name>hansaplast</name>
<uri>https://stackoverflow.com/users/119861</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/43788943/send-sigint-to-a-process-by-sending-ctrl-c-to-stdin" />
<published>2017-05-04T17:08:18Z</published>
<updated>2017-06-01T14:18:52Z</updated>
<summary type="html">
&lt;p&gt;I&#39;m looking for a way to mimick a terminal for some automated testing: i.e. start a process and then interact with it via sending data to stdin and reading from stdout. E.g. sending some lines of input to stdin including &lt;code&gt;ctrl-c&lt;/code&gt; and &lt;code&gt;ctrl-\&lt;/code&gt; which should result in sending signals to the process.&lt;/p&gt;
&lt;p&gt;Using &lt;code&gt;std::process::Commannd&lt;/code&gt; I&#39;m able to send input to e.g. &lt;code&gt;cat&lt;/code&gt; and I&#39;m also seeing its output on stdout, but sending &lt;code&gt;ctrl-c&lt;/code&gt; (as &lt;a href=&quot;http://academic.evergreen.edu/projects/biophysics/technotes/program/ascii_ctrl.htm&quot; rel=&quot;nofollow noreferrer&quot;&gt;I understand that is &lt;code&gt;3&lt;/code&gt;&lt;/a&gt;) does not cause &lt;code&gt;SIGINT&lt;/code&gt; sent to the shell. E.g. this program should terminate:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;use std::process::{Command, Stdio};
use std::io::Write;
fn main() {
let mut child = Command::new(&quot;sh&quot;)
.arg(&quot;-c&quot;).arg(&quot;-i&quot;).arg(&quot;cat&quot;)
.stdin(Stdio::piped())
.spawn().unwrap();
let mut stdin = child.stdin.take().unwrap();
stdin.write(&amp;amp;[3]).expect(&quot;cannot send ctrl-c&quot;);
child.wait();
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I suspect the issue is that sending &lt;code&gt;ctrl-c&lt;/code&gt; needs the some tty and via &lt;code&gt;sh -i&lt;/code&gt; it&#39;s only in &quot;interactive mode&quot;.&lt;/p&gt;
&lt;p&gt;Do I need to go full fledged and use e.g. &lt;a href=&quot;https://github.com/ticki/termion&quot; rel=&quot;nofollow noreferrer&quot;&gt;&lt;code&gt;termion&lt;/code&gt;&lt;/a&gt; or &lt;a href=&quot;https://github.com/jeaye/ncurses-rs&quot; rel=&quot;nofollow noreferrer&quot;&gt;&lt;code&gt;ncurses&lt;/code&gt;&lt;/a&gt;?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;: I confused shell and terminal in the original question. I cleared this up now. Also I mentioned &lt;code&gt;ssh&lt;/code&gt; which should have been &lt;code&gt;sh&lt;/code&gt;.&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44301748</id>
<re:rank scheme="https://stackoverflow.com">1</re:rank>
<title type="text">How can I deserialize an optional field with custom functions using Serde?</title>
<category scheme="https://stackoverflow.com/tags" term="json" />
<category scheme="https://stackoverflow.com/tags" term="serialization" />
<category scheme="https://stackoverflow.com/tags" term="rust" />
<category scheme="https://stackoverflow.com/tags" term="serde" />
<author>
<name>Victor Polevoy</name>
<uri>https://stackoverflow.com/users/1750757</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44301748/how-can-i-deserialize-an-optional-field-with-custom-functions-using-serde" />
<published>2017-06-01T07:57:05Z</published>
<updated>2017-06-01T14:11:38Z</updated>
<summary type="html">
&lt;p&gt;I want to serialize and deserialize a &lt;code&gt;chrono::NaiveDate&lt;/code&gt; with custom functions, but the Serde book does not cover this functionality and the code docs also do not help.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
extern crate chrono;
use chrono::NaiveDate;
mod date_serde {
use chrono::NaiveDate;
use serde::{self, Deserialize, Serializer, Deserializer};
pub fn serialize&amp;lt;S&amp;gt;(date: &amp;amp;Option&amp;lt;NaiveDate&amp;gt;, s: S) -&amp;gt; Result&amp;lt;S::Ok, S::Error&amp;gt;
where S: Serializer {
if let Some(ref d) = *date {
return s.serialize_str(&amp;amp;d.format(&quot;%Y-%m-%d&quot;).to_string())
}
s.serialize_none()
}
pub fn deserialize&amp;lt;&#39;de, D&amp;gt;(deserializer: D)
-&amp;gt; Result&amp;lt;Option&amp;lt;NaiveDate&amp;gt;, D::Error&amp;gt;
where D: Deserializer&amp;lt;&#39;de&amp;gt; {
let s: Option&amp;lt;String&amp;gt; = Option::deserialize(deserializer)?;
if let Some(s) = s {
return Ok(Some(NaiveDate::parse_from_str(&amp;amp;s, &quot;%Y-%m-%d&quot;).map_err(serde::de::Error::custom)?))
}
Ok(None)
}
}
#[derive(Debug, Serialize, Deserialize)]
struct Test {
pub i: u64,
#[serde(with = &quot;date_serde&quot;)]
pub date: Option&amp;lt;NaiveDate&amp;gt;,
}
fn main() {
let mut test: Test = serde_json::from_str(r#&quot;{&quot;i&quot;: 3, &quot;date&quot;: &quot;2015-02-03&quot;}&quot;#).unwrap();
assert_eq!(test.i, 3);
assert_eq!(test.date, Some(NaiveDate::from_ymd(2015, 02, 03)));
test = serde_json::from_str(r#&quot;{&quot;i&quot;: 5}&quot;#).unwrap();
assert_eq!(test.i, 5);
assert_eq!(test.date, None);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I know that &lt;code&gt;Option&amp;lt;chrono::NaiveDate&amp;gt;&lt;/code&gt; can be easily deserialized by Serde because Chrono has Serde support, &lt;strong&gt;but&lt;/strong&gt; I&#39;m trying to learn Serde so I want to implement it myself. When I run this code I have a error:&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;thread &#39;main&#39; panicked at &#39;called `Result::unwrap()` on an `Err` value: ErrorImpl { code: Message(&quot;missing field `date`&quot;), line: 1, column: 8 }&#39;, /checkout/src/libcore/result.rs:859
note: Run with `RUST_BACKTRACE=1` for a backtrace.
&lt;/code&gt;&lt;/pre&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44303258</id>
<re:rank scheme="https://stackoverflow.com">0</re:rank>
<title type="text">Accessing event loop via Mutex</title>
<category scheme="https://stackoverflow.com/tags" term="loops" />
<category scheme="https://stackoverflow.com/tags" term="events" />
<category scheme="https://stackoverflow.com/tags" term="rust" />
<author>
<name>LEGOlord208</name>
<uri>https://stackoverflow.com/users/5069285</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44303258/accessing-event-loop-via-mutex" />
<published>2017-06-01T09:11:49Z</published>
<updated>2017-06-01T13:47:31Z</updated>
<summary type="html">
&lt;p&gt;I&#39;m using the &lt;a href=&quot;https://crates.io/crates/discord&quot; rel=&quot;nofollow noreferrer&quot;&gt;discord&lt;/a&gt; crate, which uses event loops. I need to check for events concurrently, while I am taking user input:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;let clone = context.clone();
thread::spawn(
move || loop {
match clone.lock().unwrap().gateway.recv_event() {
Ok(event) =&amp;gt; {
// println!(&quot;Updating state: {:?}&quot;, event);
clone.lock().unwrap().state.update(&amp;amp;event)
},
Err(err) =&amp;gt; {
stderr!(&quot;Error receiving: {}&quot;, err);
},
}
}
);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This doesn&#39;t work because it stays locked... So what about&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;println!(&quot;Locking&quot;);
let mut gateway = {
&amp;amp;mut clone.lock().unwrap().gateway
};
println!(&quot;Unlocked? {:?}&quot;, clone);
match gateway.recv_event() {
Ok(event) =&amp;gt; {
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This also doesn&#39;t seem to work:&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;Locking
Unlocked? Mutex { &amp;lt;locked&amp;gt; }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;How would this be solved?&lt;/p&gt;
&lt;p&gt;It occurred to me it might just be best if there was a way to access to mutex&#39;s contents without locking.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://play.rust-lang.org/?gist=33133443d69e5ec5600a07c4add01cab&amp;amp;version=stable&amp;amp;backtrace=0&quot; rel=&quot;nofollow noreferrer&quot;&gt;Example in Playground&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.reddit.com/r/rust/comments/6e6nwu/accessing_event_loop_via_mutex/&quot; rel=&quot;nofollow noreferrer&quot;&gt;The original question is here on reddit.&lt;/a&gt;&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44305216</id>
<re:rank scheme="https://stackoverflow.com">2</re:rank>
<title type="text">Is there a way to get the type name of a trait?</title>
<category scheme="https://stackoverflow.com/tags" term="rust" />
<author>
<name>Boris</name>
<uri>https://stackoverflow.com/users/5223795</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44305216/is-there-a-way-to-get-the-type-name-of-a-trait" />
<published>2017-06-01T10:34:54Z</published>
<updated>2017-06-01T13:47:03Z</updated>
<summary type="html">
&lt;p&gt;I&#39;m trying to use &lt;code&gt;std::intrinsics::type_name&lt;/code&gt; to get the type name of a trait but can&#39;t compile it:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#![feature(core_intrinsics)]
use std::intrinsics::type_name;
trait TestTrait: Sized {
fn test(&amp;amp;self);
}
struct MyStruct {}
struct GetType {}
impl GetType {
fn test_type&amp;lt;T: ?Sized&amp;gt;() {
let test = unsafe { type_name::&amp;lt;T&amp;gt;() };
println!(&quot;{:?}&quot;, test);
}
}
fn main() {
GetType::test_type::&amp;lt;i32&amp;gt;();
GetType::test_type::&amp;lt;MyStruct&amp;gt;();
GetType::test_type::&amp;lt;TestTrait&amp;gt;();
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here is the error I get from the compiler&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;error[E0038]: the trait `TestTrait` cannot be made into an object
--&amp;gt; src/main.rs:23:30
|
23 | GetType::test_type::&amp;lt;TestTrait&amp;gt;();
| ^^^^^^^^^ the trait `TestTrait` cannot be made into an object
|
= note: the trait cannot require that `Self : Sized`
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here is the output of that test when I comment the line &lt;code&gt;GetType::test_type::&amp;lt;TestTrait&amp;gt;();&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;&quot;i32&quot;
&quot;MyStruct&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is there a way to solve this or to get the type name of a trait?&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Working solution thanks to @evotopid&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#![feature(core_intrinsics)]
use std::intrinsics::type_name;
trait TestTrait { // &amp;lt;--- remove `: Sized` constraint from here
fn test(&amp;amp;self);
}
struct MyStruct {}
struct GetType {}
impl GetType {
fn test_type&amp;lt;T: ?Sized&amp;gt;() { // &amp;lt;--- trick is in that bound
let test = unsafe { type_name::&amp;lt;T&amp;gt;() };
println!(&quot;{:?}&quot;, test);
}
}
fn main() {
GetType::test_type::&amp;lt;i32&amp;gt;();
GetType::test_type::&amp;lt;MyStruct&amp;gt;();
GetType::test_type::&amp;lt;TestTrait&amp;gt;();
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Leading to the following output&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;&quot;i32&quot;
&quot;MyStruct&quot;
&quot;TestTrait&quot;
&lt;/code&gt;&lt;/pre&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44298242</id>
<re:rank scheme="https://stackoverflow.com">0</re:rank>
<title type="text">Compile specific tests into binary</title>
<category scheme="https://stackoverflow.com/tags" term="unit-testing" />
<category scheme="https://stackoverflow.com/tags" term="testing" />
<category scheme="https://stackoverflow.com/tags" term="rust" />
<category scheme="https://stackoverflow.com/tags" term="rust-cargo" />
<author>
<name>user1413793</name>
<uri>https://stackoverflow.com/users/1413793</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44298242/compile-specific-tests-into-binary" />
<published>2017-06-01T03:52:00Z</published>
<updated>2017-06-01T13:41:21Z</updated>
<summary type="html">
&lt;p&gt;I would like to compile a binary which runs a certain subset of tests. When I run the following, it works:&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;ubuntu@ubuntu-xenial:/ox$ cargo test hash::vec
Finished dev [unoptimized + debuginfo] target(s) in 0.11 secs
Running target/debug/deps/ox-824a031ff1732165
running 9 tests
test hash::vec::test_hash_entry::test_get_offset_tombstone ... ok
test hash::vec::test_hash_entry::test_get_offset_value ... ok
test hash::vec::test_hash_table::test_delete ... ok
test hash::vec::test_hash_table::test_delete_and_set ... ok
test hash::vec::test_hash_table::test_get_from_hash ... ok
test hash::vec::test_hash_table::test_get_non_existant_from_hash ... ok
test hash::vec::test_hash_table::test_override ... ok
test hash::vec::test_hash_table::test_grow_hash ... ok
test hash::vec::test_hash_table::test_set_after_filled_with_tombstones ... ok
test result: ok. 9 passed; 0 failed; 0 ignored; 0 measured; 8 filtered out
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When I try to run &lt;code&gt;target/debug/deps/ox-824a031ff1732165&lt;/code&gt;, it runs all my tests, not just the 9 specified in &lt;code&gt;hash::vec&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I&#39;ve tried to run &lt;code&gt;cargo rustc --test hash::vec&lt;/code&gt; but I get
&lt;code&gt;error: no test target named&lt;/code&gt;hash::vec&lt;code&gt;.&lt;/code&gt;cargo rustc -- --test&lt;code&gt;works, but creates a binary that runs all tests. If I try&lt;/code&gt;cargo rustc -- --test hash::vec`, I get:&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt; Compiling ox v0.1.0 (file:///ox)
error: multiple input filenames provided
error: Could not compile `ox`.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;cargo rustc -h&lt;/code&gt; says that you can pass NAME with the &lt;code&gt;--test&lt;/code&gt; flag (&lt;code&gt;--test NAME Build only the specified test target&lt;/code&gt;), so I&#39;m wondering what &quot;NAME&quot; is and how to pass it in so I get a binary that only runs the specified 9 tests in &lt;code&gt;hash::vec&lt;/code&gt;.&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44308812</id>
<re:rank scheme="https://stackoverflow.com">1</re:rank>
<title type="text">Function argument reports &quot;error expected pattern, found keyword `mod`&quot;</title>
<category scheme="https://stackoverflow.com/tags" term="function" />
<category scheme="https://stackoverflow.com/tags" term="rust" />
<author>
<name>VikingPingvin</name>
<uri>https://stackoverflow.com/users/3975781</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44308812/function-argument-reports-error-expected-pattern-found-keyword-mod" />
<published>2017-06-01T13:25:35Z</published>
<updated>2017-06-01T13:31:01Z</updated>
<summary type="html">
&lt;p&gt;I have a barebones function in a class, this is the only function.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;pub fn getfreq(note: char, mod: char, oct: i8) {}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And I get the following error:&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;error: expected pattern, found keyword `mod`
--&amp;gt; src/main.rs:1:28
|
1 | pub fn getfreq(note: char, mod: char, oct: i8) {}
| ^^^
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is &lt;code&gt;mod&lt;/code&gt; something Rust related? I just want to use it for abbreviating &#39;modifier&#39;.&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44246722</id>
<re:rank scheme="https://stackoverflow.com">2</re:rank>
<title type="text">Is there any way to create an alias of a specific FnMut?</title>
<category scheme="https://stackoverflow.com/tags" term="rust" />
<author>
<name>user1244932</name>
<uri>https://stackoverflow.com/users/1244932</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44246722/is-there-any-way-to-create-an-alias-of-a-specific-fnmut" />
<published>2017-05-29T16:14:06Z</published>
<updated>2017-06-01T13:28:56Z</updated>
<summary type="html">
&lt;p&gt;I want to work with &lt;code&gt;FnMut(&amp;amp;[f32]) -&amp;gt; f32&lt;/code&gt;,
to not copy/paste full signature, I want to introduce some kind of alias, but&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;type Boo = FnMut(&amp;amp;[f32]) -&amp;gt; f32;
fn f&amp;lt;F: Boo&amp;gt;(mut f: F) {}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Causes a compiler error:&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;error[E0404]: expected trait, found type alias `Boo`
--&amp;gt; src/main.rs:3:13
|
3 | fn f&amp;lt;F: Boo&amp;gt;(mut f: F) {}
| ^^^ type aliases cannot be used for traits
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I then tried:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;trait Boo: FnMut(&amp;amp;[f32]) -&amp;gt; f32 {}
fn f&amp;lt;F: Boo&amp;gt;(mut f: F) {}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;it compiled, but if I try to use &lt;code&gt;Boo&lt;/code&gt; in place of trait in another place:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;trait Boo: FnMut(&amp;amp;[f32]) -&amp;gt; f32 {}
struct X(Vec&amp;lt;Box&amp;lt;Boo&amp;gt;&amp;gt;);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I get:&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;error[E0191]: the value of the associated type `Output` (from the trait `std::ops::FnOnce`) must be specified
--&amp;gt; src/main.rs:3:18
|
3 | struct X(Vec&amp;lt;Box&amp;lt;Boo&amp;gt;&amp;gt;);
| ^^^ missing associated type `Output` value
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is there any way to create an alias of a specific &lt;code&gt;FnMut&lt;/code&gt; which I can use
instead of &lt;code&gt;FnMut(&amp;amp;[f32]) -&amp;gt; f32&lt;/code&gt;?&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/39550856</id>
<re:rank scheme="https://stackoverflow.com">1</re:rank>
<title type="text">What is the right way to allocate data to pass to an FFI call?</title>
<category scheme="https://stackoverflow.com/tags" term="rust" />
<category scheme="https://stackoverflow.com/tags" term="ffi" />
<author>
<name>Norbert</name>
<uri>https://stackoverflow.com/users/2068852</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/39550856/what-is-the-right-way-to-allocate-data-to-pass-to-an-ffi-call" />
<published>2016-09-17T19:24:58Z</published>
<updated>2017-06-01T13:26:30Z</updated>
<summary type="html">
&lt;p&gt;After &lt;a href=&quot;https://stackoverflow.com/questions/39534712/should-i-pass-a-mutable-reference-or-transfer-ownership-of-a-variable-in-the-con&quot;&gt;discussing/learning about the correct way to call a FFI of the Windows-API from Rust&lt;/a&gt;, I played with it a little bit further and would like to double-check my understanding. &lt;/p&gt;
&lt;p&gt;I have a Windows API that is called twice. In the first call, it returns the size of the buffer that it will need for its actual out parameter. Then, it is called a second time with a buffer of sufficient size. I&#39;m currently using a &lt;code&gt;Vec&lt;/code&gt; as a datatype for this buffer (see example below). &lt;/p&gt;
&lt;p&gt;The code works but I&#39;m wondering whether this is right way to do it or whether it would be better to utilize a function like &lt;code&gt;alloc::heap::allocate&lt;/code&gt; to directly reserve some memory and then to use &lt;code&gt;transmute&lt;/code&gt; to convert the result from the FFI back. Again, my code works but I&#39;m trying to look a little bit behind the scenes. &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;extern crate advapi32;
extern crate winapi;
extern crate widestring;
use widestring::WideCString;
use std::io::Error as IOError;
use winapi::winnt;
fn main() {
let mut lp_buffer: Vec&amp;lt;winnt::WCHAR&amp;gt; = Vec::new();
let mut pcb_buffer: winapi::DWORD = 0;
let rtrn_bool = unsafe {
advapi32::GetUserNameW(lp_buffer.as_mut_ptr(),
&amp;amp;mut pcb_buffer )
};
if rtrn_bool == 0 {
match IOError::last_os_error().raw_os_error() {
Some(122) =&amp;gt; {
// Resizing the buffers sizes so that the data fits in after 2nd
lp_buffer.resize(pcb_buffer as usize, 0 as winnt::WCHAR);
} // This error is to be expected
Some(e) =&amp;gt; panic!(&quot;Unknown OS error {}&quot;, e),
None =&amp;gt; panic!(&quot;That should not happen&quot;),
}
}
let rtrn_bool2 = unsafe {
advapi32::GetUserNameW(lp_buffer.as_mut_ptr(),
&amp;amp;mut pcb_buffer )
};
if rtrn_bool2 == 0 {
match IOError::last_os_error().raw_os_error() {
Some(e) =&amp;gt; panic!(&quot;Unknown OS error {}&quot;, e),
None =&amp;gt; panic!(&quot;That should not happen&quot;),
}
}
let widestr: WideCString = unsafe { WideCString::from_ptr_str(lp_buffer.as_ptr()) };
println!(&quot;The owner of the file is {:?}&quot;, widestr.to_string_lossy());
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Dependencies:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[dependencies]
advapi32-sys = &quot;0.2&quot;
winapi = &quot;0.2&quot;
widestring = &quot;*&quot;
&lt;/code&gt;&lt;/pre&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44303915</id>
<re:rank scheme="https://stackoverflow.com">0</re:rank>
<title type="text">No default toolchain configured after installing rustup</title>
<category scheme="https://stackoverflow.com/tags" term="rust" />
<author>
<name>Darkaird</name>
<uri>https://stackoverflow.com/users/7546477</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44303915/no-default-toolchain-configured-after-installing-rustup" />
<published>2017-06-01T09:38:01Z</published>
<updated>2017-06-01T13:25:17Z</updated>
<summary type="html">
&lt;p&gt;I installed Rust using &lt;a href=&quot;https://www.rust-lang.org/en-US/install.html&quot; rel=&quot;nofollow noreferrer&quot; title=&quot;rustup&quot;&gt;rustup&lt;/a&gt;, but when I try to enter a Rust command like &lt;code&gt;cargo&lt;/code&gt; or &lt;code&gt;rustc&lt;/code&gt; in my console, the following error appears:&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;error: no default toolchain configured
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is this a known issue? I&#39;m using Windows 7. &lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/39219667</id>
<re:rank scheme="https://stackoverflow.com">1</re:rank>
<title type="text">How to get the index of an element in a vector using pointer arithmetic?</title>
<category scheme="https://stackoverflow.com/tags" term="pointers" />
<category scheme="https://stackoverflow.com/tags" term="vector" />
<category scheme="https://stackoverflow.com/tags" term="rust" />
<author>
<name>ideasman42</name>
<uri>https://stackoverflow.com/users/432509</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/39219667/how-to-get-the-index-of-an-element-in-a-vector-using-pointer-arithmetic" />
<published>2016-08-30T05:51:21Z</published>
<updated>2017-06-01T12:50:49Z</updated>
<summary type="html">
&lt;p&gt;In C you can use the pointer offset to get index of an element within an array, e.g.:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;index = element_pointer - &amp;amp;vector[0];
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Given a reference to an element in an array, this should be possible in Rust too.&lt;/p&gt;
&lt;p&gt;While Rust has the ability to get the memory address from vector elements, convert them to &lt;code&gt;usize&lt;/code&gt;, then subtract them - is there a more convenient/idiomatic way to do this in Rust?&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44234358</id>
<re:rank scheme="https://stackoverflow.com">0</re:rank>
<title type="text">Second gtk_main_quit() not working</title>
<category scheme="https://stackoverflow.com/tags" term="rust" />
<category scheme="https://stackoverflow.com/tags" term="gtk" />
<author>
<name>antoyo</name>
<uri>https://stackoverflow.com/users/389119</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44234358/second-gtk-main-quit-not-working" />
<published>2017-05-29T03:53:01Z</published>
<updated>2017-06-01T11:15:04Z</updated>
<summary type="html">
&lt;p&gt;In an application, I nest gtk event loops to be able to return a value in a callback.
However, I have some issues with a callback already calling &lt;code&gt;gtk_main_quit()&lt;/code&gt; because multiple calls to this function does not seem to exit as many nestings of event loop.&lt;/p&gt;
&lt;p&gt;Here is an example of my issue:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;extern crate gtk;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::thread;
use gtk::{Button, ButtonExt, ContainerExt, Continue, Inhibit, WidgetExt, Window, WindowType};
fn main() {
gtk::init().unwrap();
let window = Window::new(WindowType::Toplevel);
let quit = Arc::new(AtomicBool::new(false));
window.connect_delete_event(|_, _| {
gtk::main_quit();
Inhibit(false)
});
let button = Button::new_with_label(&quot;Click&quot;);
let quit2 = quit.clone();
button.connect_clicked(move |_| {
let quit = quit2.clone();
thread::spawn(move || {
quit.store(true, Ordering::Relaxed);
});
println!(&quot;Run&quot;);
gtk::main();
});
window.add(&amp;amp;button);
window.show_all();
gtk::idle_add(move || {
if quit.load(Ordering::Relaxed) {
println!(&quot;Quit&quot;);
gtk::main_quit();
gtk::main_quit();
quit.store(false, Ordering::Relaxed);
}
Continue(true)
});
gtk::main();
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you can see in the &lt;code&gt;gtk::idle_add&lt;/code&gt; call, I call &lt;code&gt;gtk::main_quit()&lt;/code&gt; twice, which should exit the application when the button is pressed because &lt;code&gt;gtk::main()&lt;/code&gt; was also called twice (one at the end of the &lt;code&gt;main&lt;/code&gt; function, the other one in the button clicked callback).
But the application does not exit when I click the button.&lt;/p&gt;
&lt;p&gt;The documentation of gtk seems to indicate that this is the expected behaviour:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Makes the innermost invocation of the main loop return &lt;strong&gt;when it regains control&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;(emphasis is mine)&lt;/p&gt;
&lt;p&gt;So, I believe that this does not exit the application because calling &lt;code&gt;gtk::main_quit()&lt;/code&gt; twice won&#39;t allow the gtk main loop to &quot;regain control&quot;.&lt;/p&gt;
&lt;p&gt;My question is, what should I do between the two calls to &lt;code&gt;gtk::main_quit()&lt;/code&gt; to stop the 2 nestings of event loop?&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44289831</id>
<re:rank scheme="https://stackoverflow.com">1</re:rank>
<title type="text">How to cast an enum that is a field inside a struct?</title>
<category scheme="https://stackoverflow.com/tags" term="struct" />
<category scheme="https://stackoverflow.com/tags" term="enums" />
<category scheme="https://stackoverflow.com/tags" term="casting" />
<category scheme="https://stackoverflow.com/tags" term="rust" />
<author>
<name>Robbsen</name>
<uri>https://stackoverflow.com/users/3004044</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44289831/how-to-cast-an-enum-that-is-a-field-inside-a-struct" />
<published>2017-05-31T16:14:44Z</published>
<updated>2017-05-31T17:29:33Z</updated>
<summary type="html">
&lt;p&gt;Why does casting move &lt;code&gt;self&lt;/code&gt; out of borrowed content?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#[derive(Debug)]
enum Foo {
One = 1,
Two = 2,
}
struct Bar {
f: Foo,
}
impl Bar {
fn bar(&amp;amp;mut self) {
println!(&quot;{:?}&quot;, self.f); // &quot;Two&quot;
println!(&quot;{:?}&quot;, Foo::Two as u8); // &quot;2&quot;
println!(&quot;{:?}&quot;, self.f as u8); // error
}
}
fn main() {
Bar{f: Foo::Two}.bar();
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Throws this error:&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;error[E0507]: cannot move out of borrowed content
--&amp;gt; src/main.rs:15:26
|
15 | println!(&quot;{:?}&quot;, self.f as u8); // error
| ^^^^ cannot move out of borrowed content
&lt;/code&gt;&lt;/pre&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44290099</id>
<re:rank scheme="https://stackoverflow.com">1</re:rank>
<title type="text">How to dynamically allocate an opaque FFI type? [duplicate]</title>
<category scheme="https://stackoverflow.com/tags" term="rust" />
<category scheme="https://stackoverflow.com/tags" term="ffi" />
<author>
<name>ChrisD</name>
<uri>https://stackoverflow.com/users/342618</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44290099/how-to-dynamically-allocate-an-opaque-ffi-type" />
<published>2017-05-31T16:27:01Z</published>
<updated>2017-05-31T17:04:23Z</updated>
<summary type="html">
&lt;div class=&quot;question-status question-originals-of-duplicate&quot;&gt;
&lt;p&gt;This question already has an answer here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&quot;/questions/39550856/what-is-the-right-way-to-allocate-data-to-pass-to-an-ffi-call&quot; dir=&quot;ltr&quot;&gt;What is the right way to allocate data to pass to an FFI call?&lt;/a&gt;
&lt;span class=&quot;question-originals-answer-count&quot;&gt;
2 answers
&lt;/span&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;fr&quot;&gt;&lt;a href=&quot;/questions/originals/44290099/edit&quot; title=&quot;Edit the list of duplicate questions&quot;&gt;edit&lt;/a&gt;&lt;/div&gt;
&lt;br class=&quot;cbt&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;I&#39;m using a dynamically loaded C library that requires me to allocate and manage memory at runtime for opaque objects (i.e. structures that my program only accesses indirectly). Currently I&#39;m using code that looks similar to this, where &lt;code&gt;GetObjectSize&lt;/code&gt; and &lt;code&gt;InitalizeObject&lt;/code&gt; are foreign functions:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;unsafe {
// Ask the library for the size.
let mut size: usize = 0;
let mut success = GetObjectSize(&amp;amp;mut size);
if success &amp;amp;&amp;amp; size &amp;gt; 0 {
// allocate and zero fill the memory, using a Vec.
let mut ffi_obj = vec![0u8; size];
// pass ffi_obj to the C library.
success = InitalizeObject(ffi_obj.as_ptr(), &amp;amp;size);
// ...etc, etc.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Presumably I should be wrapping the &lt;code&gt;Vec&lt;/code&gt; in a struct so that it has a less generic type? I&#39;m not sure &lt;code&gt;Vec&lt;/code&gt; is right in the first place as I&#39;d always want a fixed size which makes having a separate length and capacity redundant.&lt;/p&gt;
&lt;p&gt;So what&#39;s the preferred way to manage the memory?&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44276059</id>
<re:rank scheme="https://stackoverflow.com">1</re:rank>
<title type="text">std::ptr::Unique has no method named offset</title>
<category scheme="https://stackoverflow.com/tags" term="pointers" />
<category scheme="https://stackoverflow.com/tags" term="rust" />
<author>
<name>user1413793</name>
<uri>https://stackoverflow.com/users/1413793</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44276059/stdptrunique-has-no-method-named-offset" />
<published>2017-05-31T05:03:15Z</published>
<updated>2017-05-31T12:23:05Z</updated>
<summary type="html">
&lt;p&gt;When I try to compile the following code using the nightly compiler, I get an error:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#![feature(alloc)]
#![feature(unique)]
#![feature(heap_api)]
extern crate alloc;
use std::ptr::{Unique, self};
use alloc::heap;
use std::mem;
fn main() {
unsafe {
let align = mem::align_of::&amp;lt;i32&amp;gt;();
let elem_size = mem::size_of::&amp;lt;i32&amp;gt;();
let ptr = heap::allocate(elem_size*5, align);
let a = Unique::new(ptr as *mut _);
println!(&quot;{}&quot;, *a.offset(2));
}
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Error:&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;rustc 1.19.0-nightly (5de00925b 2017-05-29)
error: no method named `offset` found for type `std::ptr::Unique&amp;lt;_&amp;gt;` in the current scope
--&amp;gt; &amp;lt;anon&amp;gt;:16:27
|
16 | println!(&quot;{}&quot;, *a.offset(2));
| ^^^^^^
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;According to the &lt;a href=&quot;https://doc.rust-lang.org/std/ptr/struct.Unique.html#method.offset&quot; rel=&quot;nofollow noreferrer&quot;&gt;docs&lt;/a&gt;, &lt;code&gt;offset&lt;/code&gt; should be defined for &lt;code&gt;Unique&lt;/code&gt;. What am I doing wrong?&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44272404</id>
<re:rank scheme="https://stackoverflow.com">0</re:rank>
<title type="text">Value borrowed from RefCell does not live long enough [duplicate]</title>
<category scheme="https://stackoverflow.com/tags" term="rust" />
<author>
<name>fdv1</name>
<uri>https://stackoverflow.com/users/8088730</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44272404/value-borrowed-from-refcell-does-not-live-long-enough" />
<published>2017-05-30T21:40:07Z</published>
<updated>2017-05-30T23:03:06Z</updated>
<summary type="html">
&lt;div class=&quot;question-status question-originals-of-duplicate&quot;&gt;
&lt;p&gt;This question already has an answer here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&quot;/questions/30281664/returning-the-t-borrowed-from-refcellt&quot; dir=&quot;ltr&quot;&gt;Returning the T borrowed from RefCell&amp;lt;T&amp;gt;&lt;/a&gt;
&lt;span class=&quot;question-originals-answer-count&quot;&gt;
1 answer
&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;/questions/29401626/how-do-i-return-a-reference-to-something-inside-a-refcell-without-breaking-encap&quot; dir=&quot;ltr&quot;&gt;How do I return a reference to something inside a RefCell without breaking encapsulation?&lt;/a&gt;
&lt;span class=&quot;question-originals-answer-count&quot;&gt;
1 answer
&lt;/span&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;fr&quot;&gt;&lt;a href=&quot;/questions/originals/44272404/edit&quot; title=&quot;Edit the list of duplicate questions&quot;&gt;edit&lt;/a&gt;&lt;/div&gt;
&lt;br class=&quot;cbt&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;I have a question about a lifetime error when trying to use a &lt;code&gt;RefCell&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;use std::cell::RefCell;
use std::collections::HashMap;
struct Test {
values: RefCell&amp;lt;HashMap&amp;lt;u64, String&amp;gt;&amp;gt;,
}
impl Test {
fn add_value(&amp;amp;mut self, uid: u64, value: &amp;amp;String) {
self.values.borrow_mut().insert(uid, value.clone());
}
fn get_value(&amp;amp;self, uid: u64) -&amp;gt; Option&amp;lt;&amp;amp;String&amp;gt; {
self.values.borrow().get(&amp;amp;uid)
}
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Which produces the following error:&lt;/p&gt;
&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;error: borrowed value does not live long enough
--&amp;gt; src/main.rs:13:9
|
13 | self.values.borrow().get(&amp;amp;uid)
| ^^^^^^^^^^^^^^^^^^^^ does not live long enough
14 | }
| - temporary value only lives until here
|
note: borrowed value must be valid for the anonymous lifetime #1 defined on the body at 12:53...
--&amp;gt; src/main.rs:12:54
|
12 | fn get_value(&amp;amp;self, uid: u64) -&amp;gt; Option&amp;lt;&amp;amp;String&amp;gt; {
| ______________________________________________________^ starting here...
13 | | self.values.borrow().get(&amp;amp;uid)
14 | | }
| |_____^ ...ending here
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I must be ignoring some rather fundamental piece of information.&lt;/p&gt;
</summary>
</entry>
<entry>
<id>https://stackoverflow.com/q/44254995</id>
<re:rank scheme="https://stackoverflow.com">7</re:rank>
<title type="text">Are there any performance advantages in using `unreachable!` vs `panic!`?</title>
<category scheme="https://stackoverflow.com/tags" term="rust" />
<author>
<name>John</name>
<uri>https://stackoverflow.com/users/2849934</uri>
</author>
<link rel="alternate" href="https://stackoverflow.com/questions/44254995/are-there-any-performance-advantages-in-using-unreachable-vs-panic" />
<published>2017-05-30T06:28:29Z</published>
<updated>2017-05-30T12:34:06Z</updated>
<summary type="html">
&lt;p&gt;Is the existence of &lt;code&gt;unreachable!&lt;/code&gt; macro purely for the sake of clarity when reading code, or does it provide any functional advantages?&lt;/p&gt;
</summary>
</entry>
</feed>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment