lifetime

How can two Python objects have same id but 'is' operator returns False?

*爱你&永不变心* 提交于 2019-12-20 03:16:47
问题 id(t+t), id(t*2) (42838592, 42838592) (t+t) is (t*2) False If two variable point to same object 'is' operator will return true.But first line said both have same id but 'is' operator give false value. 回答1: In the first example, your objects don't overlap in time: one is created then destroyed, then another is created with the same id. When you compare them with is , you are holding onto both objects, so they get different ids. 回答2: As explained by Ned Batchelder's answer, and by the docs for

Why does cloning data inside a closure not prevent the error “closure may outlive the current function”?

会有一股神秘感。 提交于 2019-12-20 02:15:08
问题 I built a GTK application with gtk-rs. When I build the main window, I want to use some dynamic parameters such as window height. I created a struct which contains all such settings and want to use this as an input parameter for the function building the UI: fn main() { let application = gtk::Application::new(Some("id"), Default::default()) .expect("Initialization failed..."); let config = Config {width: 100., height: 100.}; application.connect_activate(|app| { build_ui(app, config.clone());

Exception object lifetime

孤街浪徒 提交于 2019-12-19 16:34:18
问题 I want to know how the exception object is created ? and why the handler function parameter can be a non-const reference? For example: class E{ public: const char * error; E(const char* arg):error(arg){ cout << "Constructor of E(): ";} E(const E& m){ cout << "Copy constructor E(E& m): " ; error=m.error; } }; int main(){ try{ throw E("Out of memory"); } catch(E& e){cout << e.error;} } Output: Constructor of E(): Out of memory so I have throw E("out of memory") and E("out of memory") is just a

Value does not live long enough

好久不见. 提交于 2019-12-19 14:55:28
问题 I don't completely understand lifetimes, but I think b 's lifetime will end before self 's. So, how to edit this code? Do I copy something in memory? If I make a new instance, this lifetime must adhere to this case. pub struct Formater { layout: &'static str, } impl Formater { pub fn new(layout: &'static str) -> Formater { let regex = Regex::new(r"%\{([a-z]+)(?::(.*?[^\\]))?\}").unwrap(); let b = regex.replace_all(layout, "{}"); return Formater { layout: &b, }; } } The error: error: `b` does

missing lifetime specifier [E0106] on type alias

我们两清 提交于 2019-12-19 07:51:10
问题 This code: use std::fmt; use std::result::Result::{self, Ok, Err}; #[derive(Clone)] #[derive(Copy)] enum Tile { White, Black, Empty } type Board = &[[Tile; 19]; 19]; Produces this error: Compiling go v0.1.0 (file:///home/max/gits/go_rusty) src/main.rs:12:14: 12:31 error: missing lifetime specifier [E0106] src/main.rs:12 type Board = &[[Tile; 19]; 19]; ^~~~~~~~~~~~~~~~~ error: aborting due to previous error Could not compile `go`. To learn more, run the command again with --verbose. I'm having

Why can I not return a mutable reference to an outer variable from a closure?

风格不统一 提交于 2019-12-19 06:22:21
问题 I was playing around with Rust closures when I hit this interesting scenario: fn main() { let mut y = 10; let f = || &mut y; f(); } This gives an error: error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements --> src/main.rs:4:16 | 4 | let f = || &mut y; | ^^^^^^ | note: first, the lifetime cannot outlive the lifetime as defined on the body at 4:13... --> src/main.rs:4:13 | 4 | let f = || &mut y; | ^^^^^^^^^ note: ...so that closure can access

Why can I not return a mutable reference to an outer variable from a closure?

风流意气都作罢 提交于 2019-12-19 06:22:09
问题 I was playing around with Rust closures when I hit this interesting scenario: fn main() { let mut y = 10; let f = || &mut y; f(); } This gives an error: error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements --> src/main.rs:4:16 | 4 | let f = || &mut y; | ^^^^^^ | note: first, the lifetime cannot outlive the lifetime as defined on the body at 4:13... --> src/main.rs:4:13 | 4 | let f = || &mut y; | ^^^^^^^^^ note: ...so that closure can access

Why does the variable not live long enough?

冷暖自知 提交于 2019-12-19 05:47:29
问题 Consider this function that should return the file extension of a given Path . pub fn get_extension<'a>(path: &'a Path) -> Option<&'a str> { let path_str = path.as_str().unwrap(); let ext_pos = regex!(".[a-z0-9]+$").find(path_str); match ext_pos { Some((start, _)) => { return Some(path_str.as_slice().slice_from(start)) }, None => return None } } The error message is as follows: `path_str` does not live long enough The error message is pretty clear and it's a shame I can't work it out on my

Can any of existing IoC containers create the lazy proxy classes dynamically?

夙愿已清 提交于 2019-12-19 04:07:14
问题 I study different DI patterns. And now I interested in the lazy life-time implementation. For example, I want to write a proxy class that hides the factory behind a service's interface. Can any of existing IoC containers (.NET) create this kind of proxy class dynamically at runtime? interface IService { void Foo(); void Bar(); } class ServiceFactoryProxy : IService { private readonly Func<IService> _factory; public ServiceFactoryProxy(Func<IService> factory) { if (factory == null) throw new

variable does not live long enough when storing a csv::DecodedRecords iterator

痴心易碎 提交于 2019-12-18 09:26:08
问题 I'm trying to create an iterator trait that provides a specific type of resource, so I can implement multiple source types. I'd like to create a source for reading from a CSV file, a binary etc.. I'm using the rust-csv library for deserializing CSV data: #[derive(RustcDecodable)] struct BarRecord { bar: u32 } trait BarSource : Iterator {} struct CSVBarSource { records: csv::DecodedRecords<'static, std::fs::File, BarRecord>, } impl CSVBarSource { pub fn new(path: String) -> Option<CSVBarSource