use core::{fmt, panic};
use ferris_says::say;
use std::{
env, fmt::Display, fs::{self, File}, io::{self, stdout, BufWriter, ErrorKind, Read}, vec, process
};
use std::error::Error;
use hello_rust_spanti::Config;
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
}
fn add<T: std::ops::Add<Output = T>>(a: T, b: T) -> T {
a + b
}
fn test() {
let stdout = stdout();
let message = String::from("Hello 114514\t\t\t!");
let width = message.chars().count();
let arr = vec![1, 2, 3];
let mut writer = BufWriter::new(stdout.lock());
say(&message, width, &mut writer).unwrap();
say(&format!("{:?}", arr), width, &mut writer).unwrap();
say(&format!("1 + 2 = {}", add(1, 2)), width, &mut writer).unwrap();
}
fn reverse(pair: (i32, bool)) -> (bool, i32) {
let (int_param, bool_param) = pair;
(bool_param, int_param)
}
#[derive(Debug)]
struct Matrix(f32, f32, f32, f32);
impl fmt::Display for Matrix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({},{})\n({},{})", self.0, self.1, self.2, self.3)
}
}
fn first_word(s: &str) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[0..i];
}
}
&s[..]
}
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
fn can_hold(&self, other: &Rectangle) -> bool {
self.width > other.width && self.height > other.height
}
}
fn test2() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
let rect2 = Rectangle {
width: 10,
height: 40,
};
let rect3 = Rectangle {
width: 60,
height: 45,
};
println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3));
}
enum IpAddr {
V4(u8,u8,u8,u8),
V6(String),
}
#[derive(Debug)] enum UsState {
Alabama,
Alaska,
}
enum Coin {
Penny,
Nickel,
Dime,
Quarter(UsState),
}
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => {
println!("Lucky penny!");
1
}
Coin::Dime => 5,
Coin::Nickel => 10,
Coin::Quarter(state) => {
println!("State quarter from {:?}!", state);
25
}
}
}
fn plus_one(x: Option<i32>) -> Option<i32> {
match x {
None => None,
Some(i) => Some(i + 1),
}
}
macro_rules! square {
($x:expr) => {
$x * $x
};
}
fn test3(){
let five = Some(5);
let six = plus_one(five);
let none = plus_one(None);
println!("six:{:?}, none:{:?}", six, none);
let add_one = |x: i32| x + 114514;
println!("add_one: {}", add_one(1));
}
fn test4() {
let f = File::open("hello.txt");
let f = match f {
Ok(file) => file,
Err(error) => match error.kind() {
ErrorKind::NotFound => match File::create("hello.txt"){
Ok(fc) => fc,
Err(e) => panic!("Problem creating the file: {:?}", e),
},
other_error => panic!("Problem creating the file: {:?}", other_error),
},
};
}
struct Point<T>{
x: T,
y: T,
}
impl <T> Point<T>{
fn x(&self) -> &T{
&self.x
}
}
impl Point<f32>{
fn distance_from_origin(&self) -> f32{
(self.x.powi(2) + self.y.powi(2)).sqrt()
}
}
fn largest<T: PartialOrd + Copy>(list: &[T]) -> T {
let mut largest = list[0];
for &item in list.iter() {
if item > largest {
largest = item;
}
}
largest
}
fn test5() {
let number_list = vec![34, 50, 25, 100, 65];
let result = largest(&number_list);
println!("The largest number is {result}");
let number_list = vec![102, 34, 6000, 89, 54, 2, 43, 8];
let result = largest(&number_list);
println!("The largest number is {result}");
}
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 std::fmt::Display for Tweet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.username, self.content)
}
}
impl Summary for Tweet{
fn summarize(&self) -> String {
format!("{}", self)
}
}
pub fn notify<T: Summary + Display>(item: T){
println!("Breaking news! {}", item.summarize());
}
fn test6(){
let tweet = Tweet {
username: String::from("horse_ebooks"),
content: String::from("of course, as you probably already know, people"),
reply: false,
retweet: false,
};
notify(tweet);
}
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
fn test7() {
let string1 = String::from("long string is long");
{
let string2 = String::from("xyz");
let result = longest(string1.as_str(), string2.as_str());
println!("The longest string is {}", result);
}
}
struct ImportantExcerpt<'a>{
part: &'a str,
}
struct Counter {
count: usize,
}
impl Iterator for Counter {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
self.count += 1;
if self.count < 6 {
Some(self.count)
}
else{
None
}
}
}
fn main(){
let sum: usize = Counter { count:0}.sum();
println!("sum: {}", sum);
}