rust

How do I get information from an entry on button click?

青春壹個敷衍的年華 提交于 2020-01-24 02:08:17
问题 I want to get an input from an entry on a button click and display that information when another button is clicked. This gives me an error because the closure takes ownership of my firstname variable, in which I want to store the information. How do I get the information out of the entry and reuse it? // import gtk libs extern crate gio; extern crate gtk; // declare use of gtk use gtk::prelude::*; fn main() { let mut firstname = String::new(); if gtk::init().is_err() { println!("Failed to

Is it possible to use Box with no_std?

北慕城南 提交于 2020-01-23 19:24:11
问题 I'd like to use Box in a crate with no_std . Is this possible? My simple attempts so far have not worked. This compiles (but uses the standard library): fn main() { let _: Box<[u8]> = Box::new([0; 10]); } This does not: #![no_std] fn main() { let _: Box<[u8]> = Box::new([0; 10]); } (Playground) However, looking through the Rust source code, I see Box is defined in liballoc with the warning This library, like libcore, is not intended for general usage, but rather as a building block of other

How to tell what “features” are available per crate?

空扰寡人 提交于 2020-01-23 13:03:49
问题 Is there a standard way to determine what features are available for a given crate? I'm trying to read Postgres timezones, and this says to use the crate postgres = "0.17.0-alpha.1" crate's with-time or with-chrono features. When I try this in my Cargo.toml: [dependencies] postgres = { version = "0.17.0-alpha.1", features = ["with-time"] } I get this error: error: failed to select a version for `postgres`. ... required by package `mypackage v0.1.0 (/Users/me/repos/mypackage)` versions that

What is the difference between From::from and as in Rust?

人走茶凉 提交于 2020-01-23 11:16:30
问题 I can cast between types by using either from or as : i64::from(42i32); 42i32 as i64; What is the difference between those? 回答1: as can only be used in a small, fixed set of transformations. The reference documents as: as can be used to explicitly perform coercions, as well as the following additional casts. Here *T means either *const T or *mut T . | Type of e | U | Cast performed by e as U | | Integer or Float type | Integer or Float type | Numeric cast | | C-like enum | Integer type | Enum

Return reference with lifetime of self

天大地大妈咪最大 提交于 2020-01-23 07:59:46
问题 I'd like to write some code like the following: struct Foo { foo: usize } impl Foo { pub fn get_foo<'a>(&'a self) -> &'self usize { &self.foo } } But this doesn't work, failing with invalid lifetime name: 'self is no longer a special lifetime . How can I return a reference that lives as long as the object itself? 回答1: You don't want the reference to live exactly as long as the object. You just want a borrow on the object (quite possibly shorter than the entire lifetime of the object), and you

Return reference with lifetime of self

前提是你 提交于 2020-01-23 07:59:06
问题 I'd like to write some code like the following: struct Foo { foo: usize } impl Foo { pub fn get_foo<'a>(&'a self) -> &'self usize { &self.foo } } But this doesn't work, failing with invalid lifetime name: 'self is no longer a special lifetime . How can I return a reference that lives as long as the object itself? 回答1: You don't want the reference to live exactly as long as the object. You just want a borrow on the object (quite possibly shorter than the entire lifetime of the object), and you

What's the difference between a trait's generic type and a generic associated type?

独自空忆成欢 提交于 2020-01-23 05:45:08
问题 This question is asked before generic associated types are available in Rust, although they are proposed and developed. My understanding is that trait generics and associated types differ in the number of types which they can bind to a struct. Generics can bind any number of types: struct Struct; trait Generic<G> { fn generic(&self, generic: G); } impl<G> Generic<G> for Struct { fn generic(&self, _: G) {} } fn main() { Struct.generic(1); Struct.generic("a"); } Associated types bind exactly 1

What is the difference between storing a Vec vs a Slice?

房东的猫 提交于 2020-01-23 05:42:27
问题 Rust provides a few ways to store a collection of elements inside a user-defined struct. The struct can be given a custom lifetime specifier, and a reference to a slice: struct Foo<'a> { elements: &'a [i32] } impl<'a> Foo<'a> { fn new(elements: &'a [i32]) -> Foo<'a> { Foo { elements: elements } } } Or it can be given a Vec object: struct Bar { elements: Vec<i32> } impl Bar { fn new(elements: Vec<i32>) -> Bar { Bar { elements: elements } } } What are the major differences between these two

How can I convert a float to string?

ぃ、小莉子 提交于 2020-01-23 05:42:13
问题 How can a float value be converted to a String? For whatever reason, the documentation and all online sources I can find are only concerned with the other way around. let value: f32 = 17.65; let value_as_str: String = ..... 回答1: Sometimes, the answer is easy: to_string(). let pi = 3.1415926; let s = pi.to_string(); // : String Background The foundation for "creating a readable string representation of something" is in the fmt module. Probably the most important trait in this module is Display

Why does _ destroy at the end of statement?

那年仲夏 提交于 2020-01-23 05:30:53
问题 I've seen a few other questions and answers stating that let _ = foo() destroys the result at the end of the statement rather than at scope exit, which is what let _a = foo() does. I am unable to find any official description of this, nor any rationale for this syntax. I'm interested in a few inter-twined things: Is there even a mention of it in the official documentation? What is the history behind this choice? Is it simply natural fall-out from Rust's binding / destructuring rules? Is it