Created
April 11, 2024 20:05
-
-
Save jonathanwork/d11cdb59a311836a18f3dc136ba16de5 to your computer and use it in GitHub Desktop.
running simple demos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// use anyhow::Error; | |
trait NonDis { | |
fn foo() where Self: Sized {} | |
fn returns (&self ) -> Self where Self: Sized; | |
fn param(&self, other: Self) where Self: Sized {} | |
fn typed<T> ( &self, x : T) where Self: Sized {} | |
} | |
struct S; | |
impl NonDis for S { | |
fn returns(&self) -> Self where Self : Sized { S } | |
} | |
// fn say_hello(out: &mut dyn Write) -> std::io::Result<()> { | |
// out.write_all(b"hello world\n" ) ; | |
// out.flush() | |
// } | |
pub trait Summary { | |
fn summarize(&self) -> String; | |
} | |
pub struct NewsArticle { | |
pub headline: String, | |
pub location: String, | |
pub author: String, | |
pub content: String, | |
} | |
impl Summary for NewsArticle { | |
fn summarize(&self) -> String { | |
format!("{}, by {}, ({})", self.headline, self.author, self.location) | |
} | |
} | |
pub struct Tweet { | |
pub username: String, | |
pub content: String, | |
pub reply: bool, | |
pub retweet: bool, | |
} | |
impl Summary for Tweet { | |
fn summarize(&self) -> String { | |
format!("{}: {}", self.username, self.content) | |
} | |
} | |
pub fn notify(item : &impl Summary ) { | |
println!("breaking {}" , item.summarize()) | |
} | |
fn main() { | |
let tweet = Tweet { | |
username: String::from("horse_ebooks"), | |
content: String::from( | |
"of course, as you probably already know, people", | |
), | |
reply: false, | |
retweet: false, | |
}; | |
println!("1 new tw : {}" , tweet.summarize() ) ; | |
notify(&tweet) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment