赞
踩
struct 结构体名 {
数据1:数据类型1,
数据2:数据类型2,
...
}
struct Student {
name:String,
age:u32,
grade:String,
state:bool
}
fn main() { let s1 = Student{ name:String::from("张三"), age:23, grade:String::from("高三"), state:true }; println!("学生{},年纪{},年级{},在学状态{}", s1.name, s1.age, s1.grade, s1.state); } struct Student { name:String, age:u32, grade:String, state:bool } // 输出: // 学生张三,年纪23,年级高三,在学状态true
fn main() { let mut s1 = Student{ name:String::from("张三"), age:23, grade:String::from("高三"), state:true }; println!("学生{},年纪{},年级{},在学状态{}", s1.name, s1.age, s1.grade, s1.state); s1.grade = String::from("大二"); println!("学生{},年纪{},年级{},在学状态{}", s1.name, s1.age, s1.grade, s1.state); } struct Student { name:String, age:u32, grade:String, state:bool } /*输出 学生张三,年纪23,年级高三,在学状态true 学生张三,年纪23,年级大二,在学状态true */
fn main() { let s2 = build_student(String::from("御承扬"), 21, String::from("大三"), true); println!("学生{},年纪{},年级{},在学状态{}", s2.name, s2.age, s2.grade, s2.state); } struct Student { name: String, age: u32, grade: String, state: bool, } fn build_student(_name: String, _age: u32, _grade: String, _state: bool) -> Student { Student { name: _name, age: _age, grade: _grade, state: _state, } } // 输出 // 学生御承扬,年纪21,年级大三,在学状态true
fn build_student2(name:String, age:u32, grade:String, state:bool) -> Student {
Student{
name,
age,
grade,
state
}
}
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
let black = Color(0, 0, 0);
let origin = Point(0, 0, 0);
fn main() {
let width1 = 30;
let height1 = 50;
println!(
"The area of the rectangle is {} square pixels.",
area(width1, height1)
);
}
fn area(width: u32, height: u32) -> u32 {
width * height
}
fn main() {
let rect1 = (30, 50);
println!(
"The area of the rectangle is {} square pixels.",
area(rect1)
);
}
fn area(dimensions: (u32, u32)) -> u32 {
dimensions.0 * dimensions.1
}
struct Rectangle { width: u32, height: u32, } fn main() { let rect1 = Rectangle { width: 30, height: 50 }; println!( "The area of the rectangle is {} square pixels.", area(&rect1) ); } fn area(rectangle: &Rectangle) -> u32 { rectangle.width * rectangle.height }
The area of the rectangle is 1500 square pixels.
error[E0277]: the trait bound `Rectangle: std::fmt::Display` is not satisfied
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let rect1 = Rectangle { width: 30, height: 50 };
println!("rect1 is {:?}", rect1);
}
// 输出:
// rect1 is Rectangle { width: 30, height: 50 }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。