traits

How does the mechanism behind the creation of boxed traits work?

孤者浪人 提交于 2019-12-18 06:54:06
问题 I'm having trouble understanding how values of boxed traits come into existence. Consider the following code: trait Fooer { fn foo(&self); } impl Fooer for i32 { fn foo(&self) { println!("Fooer on i32!"); } } fn main() { let a = Box::new(32); // works, creates a Box<i32> let b = Box::<i32>::new(32); // works, creates a Box<i32> let c = Box::<Fooer>::new(32); // doesn't work let d: Box<Fooer> = Box::new(32); // works, creates a Box<Fooer> let e: Box<Fooer> = Box::<i32>::new(32); // works,

Cannot use moved BufReader after for loop with bufreader.lines()

怎甘沉沦 提交于 2019-12-18 06:39:38
问题 I'm trying to read some lines from a file, skipping the first few and printing the rest, but I keep getting errors about used value after move: use std::fs::File; use std::io::{self, BufRead, BufReader, Read}; use std::path::Path; fn skip_and_print_file(skip: &usize, path: &Path) { let mut skip: usize = *skip; if let Ok(file) = File::open(path) { let mut buffer = BufReader::new(file); for (index, line) in buffer.lines().enumerate() { if index >= skip { break; } } print_to_stdout(&mut buffer);

Cannot use moved BufReader after for loop with bufreader.lines()

六眼飞鱼酱① 提交于 2019-12-18 06:37:06
问题 I'm trying to read some lines from a file, skipping the first few and printing the rest, but I keep getting errors about used value after move: use std::fs::File; use std::io::{self, BufRead, BufReader, Read}; use std::path::Path; fn skip_and_print_file(skip: &usize, path: &Path) { let mut skip: usize = *skip; if let Ok(file) = File::open(path) { let mut buffer = BufReader::new(file); for (index, line) in buffer.lines().enumerate() { if index >= skip { break; } } print_to_stdout(&mut buffer);

Is it better to specify trait bound on the impl block or on the method?

前提是你 提交于 2019-12-18 05:57:27
问题 Suppose I want to create some type that wraps some other generic type, like so: struct MyWrapper<T> { pub inner: T, } Now I want my type to have a method if the inner type satisfies a specific bound. For example: I want to print it (in this example without using fmt traits for simplicity). To do this I have two possibilities: adding a bound to the impl or to the method itself. Method Bound impl<T> MyWrapper<T> { pub fn print_inner(&self) where T: std::fmt::Display { println!("[[ {} ]]", self

Is a C++ is_lambda trait, purely implemented as a library, impossible?

霸气de小男生 提交于 2019-12-18 05:45:12
问题 I have a question regarding C++0x lambdas. In my code, it would be beneficial to know whether or not a given type is the type of a C++0x lambda expression. To give an example: struct foobar { void operator()() { } }; auto lambda = []{}; typedef is_lambda < decltype(lambda) > ::type T; // T would be a true_type typedef is_lambda < foobar > ::type T; // T would be a false_type It is rather easy to distinguish lambda expressions from function and member function types. Functors are another

Scala case class extending Product with Serializable

房东的猫 提交于 2019-12-17 21:56:28
问题 I am learning scala and tried following form Scala Cookbook: trait Animal trait FurryAnimal extends Animal case class Dog(name:String) extends Animal case class Cat(name:String) extends Animal Now when I did following as : val x = Array(Dog("Fido"),Cat("Felix")) it show result as : x:Array[Product with Serializable with Animal] = Array(Dog(Fido),Cat(Felix)) Although I know that a case class is mixed in with Product trait What I am not getting is : Product with Serializable with Animal As per

Multiple inheritance using classes

牧云@^-^@ 提交于 2019-12-17 21:28:52
问题 Is it possible to extend only some specific part from multiple classes? Example: class Walker { walk() { console.log("I am walking"); } // more functions } class Runner { run() { console.log("I am running"); } // more functions } // Make a class that inherits only the run and walk functions 回答1: You can pick and choose which methods from other classes you want to add to an existing class as long as your new object has the instance data and methods that the methods you are adding expect to be

Why does a reference to a trait in a generic function have to implement `Sized`?

百般思念 提交于 2019-12-17 19:54:54
问题 I have a function that returns a reference to a trait ( trait_ref() ) and another function that takes a reference to a generic trait implementation ( take_trait_ref_generic ). However, it's not possible to pass the reference I get from the first function to the second one. Rustc complains that "the trait std::marker::Sized is not implemented for SomeTrait ". Even though that's true, why does it have to implement Sized ? It's a reference anyway. trait SomeTrait {} struct TraitImpl; impl

Difference between trait inheritance and self type annotation

梦想与她 提交于 2019-12-17 18:13:23
问题 In Scala, I've seen the constructs trait T extends S and trait T { this: S => used to achieve similar things (namely that the abstract methods in S must be defined before an instance may be created). What's the difference between them? Why would you use one over the other? 回答1: I'd use self-types for dependency-management: This trait requires another trait to be mixed in. And I'd use inheritance to refine another trait or interface. Just as an example: trait FooService trait FooRemoting {

Is it possible to extend a default method implementation of a trait in a struct?

淺唱寂寞╮ 提交于 2019-12-17 16:27:37
问题 In traditional object-oriented languages (e.g. Java), it is possible to "extend" the functionality of a method in an inherited class by calling the original method from the super class in the overridden version, for example: class A { public void method() { System.out.println("I am doing some serious stuff!"); } } class B extends A { @Override public void method() { super.method(); // here we call the original version System.out.println("And I'm doing something more!"); } } As you can see, in