How to write a function that returns a RangeInclusive or its reverse iterator in stable Rust?

I have this code use std::iter::Step; fn main() { for i in mkiter(25, 20) { println!("{}", i); } for i in mkiter(10, 23) { println!("{}", i); } } fn mkiter<T>(start: T, end: T) -> Box<dyn Iterator<Item = T>> where T: Step, { if start > end { Box::new((end..=start).rev()) } else { Box::new(start..=end) } } However,… Read More How to write a function that returns a RangeInclusive or its reverse iterator in stable Rust?

error: the trait bound `someStruct<std::fs::File>: Write<std::fs::File>` is not satisfied

I am trying to create a generic wrapper type to simplify so branching logic however the compiler complains that the Write trait is not implemented for the specific type being used in that function. Not pictured here is the logic of someFunction requires the inner type to do some instructions specific to itself to finish… Read More error: the trait bound `someStruct<std::fs::File>: Write<std::fs::File>` is not satisfied

Create flexible constructor method accepting either String or &str

I am completely new to rust. I created a program to store information about persons to get in touch with the language: person.rs pub struct Person { firstname: String, lastname: String, pub age: u8 } impl Person { pub fn new(firstname: String, lastname: String, age: u8) -> Person { return Person { firstname: firstname, lastname:… Read More Create flexible constructor method accepting either String or &str

How can I import `str::to_string` in Rust? (And is it possible to do this with trait methods in general?)

I want to turn a &str into a String which can be done simply with the method let res = "my_string".to_string() I prefer thinking of methods as acting directly on a structure so I’d like to write let res = to_string("my_string"). The closest I’ve come to this so far is: str::to_string("my_string") but providing the namespace… Read More How can I import `str::to_string` in Rust? (And is it possible to do this with trait methods in general?)

How to have a trait method have a struct argument, in which an item must implement that same trait?

I have a struct Lambertian: pub struct Lambertian { albedo: Color, } A Material trait with an associated method: pub trait Material { fn scatter(&self, ray: &Ray, rec: &HitRecord) -> Option<(Ray, Color)>; } And finally, another struct that holds a value of something that implements the Material trait pub struct HitRecord<T> where T: Material {… Read More How to have a trait method have a struct argument, in which an item must implement that same trait?

Method returning dyn fails to compile if the return value is assigned to a variable before returned

After struggling for hours with writing a factory method which returns instances of a struct containing a field of a generic type (implementing a trait), I find that it compiles and runs only if I do not assign the result to a variable before returning it (see the bottom of the post for a complete… Read More Method returning dyn fails to compile if the return value is assigned to a variable before returned