pub struct AA {
field: i32,
a:i32
}
impl AA {
fn default() -> AA {
AA {field:0, a:0}
}
pub fn new(value: i32,a1:i32) -> AA {
AA {
field: value,
a:a1
}
}
pub fn test1(&self) {
println!("from 8888888 value={}",self.field);
self.test();
}
fn test(&self) {
println!("from 44444444");
}
pub fn getA(&self)->i32{
return self.a;
}
pub fn setA(&mut self,t:i32){
self.a=t;
}
}
/*...................................................... */
trait Shape {
fn area(&self) -> f64;
}
struct Circle {
radius: f64,
}
impl Shape for Circle {
fn area(&self) -> f64 {
return 3.14159 * self.radius * self.radius
}
}
impl Circle {
pub fn test1(&self) -> f64 {
return self.radius ;
}
}
struct Rectangle {
width: f64,
height: f64,
}
impl Shape for Rectangle {
fn area(&self) -> f64 {
self.width * self.height
}
}
// 使用泛型函数展示多态
fn print_area<T: Shape>(shape: &T) {
println!("Area: {}", shape.area());
}
fn main() {
let mut a = AA::default();
// let mut a = AA::new(777,44);
a.test1();
a.test();
a.setA(888);
print!("A={}\n",a.getA());
let circle: Circle = Circle { radius: 2.0 };
let rectangle = Rectangle { width: 3.0, height: 4.0 };
print_area(&circle);
print_area(&rectangle);
println!("{}",circle.test1());
/**************************************************** */
fn add(a :i32,b:i32)->i32{
return a+b;
}
//let add = |a, b| a + b;
let add1 = |a, b| a + b;
println!("{} {}", add(2, 3),add1(1,1));
let t=4;
let sp=|x|x+t;
println!("{}",sp(100));
}
rust way step 6
于 2024-07-11 10:55:41 首次发布