Commit 70f735cc by 邢兆忠

Initial commit

parents
Pipeline #4616 failed with stages
in 0 seconds
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "trafficlight"
version = "0.1.0"
[package]
name = "trafficlight"
version = "0.1.0"
authors = ["邢兆忠 <zhaozhong.xing@hrtuoyu.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
use std::io;
struct Traffic{}
/**
*
*/
struct Area{
shape_type: String,
shape_base: f32,
shape_height: f32,
}
fn main() {
//交通信号灯
let light = Traffic{};
for _ in 0..2 {
let mut input = String::new();
io::stdin().read_line(&mut input).expect("failed to read line");
// print!("{:?}", input);
println!("{} light is {}s",input.trim(), light.light_time(input.to_string()));
}
// println!("{} light is {}s",input.to_string(), light.light_time("red"));
//定义整数集合
let number_list = &[30, 50, 78, 96];
//整数求和
let result = sum_u32(number_list);
//println!("number_list sum is {}", result);
// match result {
// // The division was valid
// Some(x) => println!("Result: {}", x),
// // The division was invalid
// None => println!("Cannot sum")
// }
println!("{:?}", result);
let area = Area{
shape_type:String::from("三角形"),
shape_base: 3.0,
shape_height: 5.0,
};
println!("{}------------{}", area.shape_type, calculate(&area));
}
trait TrafficLight{
fn light_time(&self, _type : String)->u8;
}
/**
* 交通信号灯
*/
impl TrafficLight for Traffic{
fn light_time(&self, _type : String) ->u8{
// println!("light type {}", _type == "red\n");
if _type == "red\n" {
60
}else if _type == "green\n" {
10
}else if _type == "yellow\n"{
20
}else{
0
}
}
}
/**
* 整数求和
* */
fn sum_u32(list:&[u32])->Option<u32>{
let len = list.len();
let mut sum_u32 = 0;
if len== 0 {
None
} else {
for item in list {
sum_u32 += item;
}
Some(sum_u32)
}
}
pub trait MultArea{
fn mult (&self)->f32;
}
impl MultArea for Area{
fn mult(&self)->f32{
if self.shape_type == "正方形" {//正方形
self.shape_base * self.shape_height
}else if self.shape_type == "圆形" {//圆形
self.shape_base * self.shape_height * 3.14
}else if self.shape_type == "三角形" {//三角形
self.shape_base * self.shape_height / 2.0
}else{
0.0
}
}
}
pub fn calculate<T: MultArea> (item: &T)->f32{
item.mult()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment