目录定义struct实例化struct取得struct里面的某个值struct作为函数的放回值字段初始化简写struct更新语法tuple structUnit-Like Struc
struct User{
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
struct User{
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
fn main(){
let user1 = User{
username: String::from("xxxx"),
email: String::from("xxxx@163.com"),
active: true,
sign_in_count:556,
};
println!("username:{}", user1.username);
println!("email:{}", user1.email);
println!("active:{}", user1.active);
println!("sign_in_count:{}", user1.sign_in_count);
}
使用点标记法
struct User{
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
fn main(){
let mut user1 = User{
username: String::from("xxxx"),
email: String::from("xxxx@163.com"),
active: true,
sign_in_count:556,
};
// 注意 user1 必须是可变的
user1.username = String::from("hhhhhhh");
println!("username:{}", user1.username);
println!("email:{}", user1.email);
println!("active:{}", user1.active);
println!("sign_in_count:{}", user1.sign_in_count);
}
注意
一旦struct的实例是可变的,那么实例中的所有字段都是可以变的
struct User{
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
fn build_user(email: String, username: String)-> User{
User{
email: email,
username: username,
active: true,
sign_in_count:1,
}
}
fn main(){
let email = String::from("xxxx@163.com");
let username = String::from("llllll");
let user1 = build_user(email, username);
println!("username:{}", user1.username);
println!("email:{}", user1.email);
println!("active:{}", user1.active);
println!("sign_in_count:{}", user1.sign_in_count);
}
当字段名与字段值对应变量名相同时,就可以使用字段初始化简写的方式
fn build_user(email: String, username: String)-> User{
User{
email,
username,
active: true,
sign_in_count:1,
}
}
当你想基于某个struct实例来创建一个新实例的时候,可以使用struct更新语法
struct User{
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
fn build_user(email: String, username: String)-> User{
User{
email,
username,
active: true,
sign_in_count:1,
}
}
fn main(){
let email = String::from("xxxx@163.com");
let username = String::from("llllll");
let user1 = build_user(email, username);
// user2 email 重新赋值
// user2 其他变量 使用 user1 的值
// String 类型会被引用 从而失效
let user2 = User{
email: String::from("user2@163.com"),
..user1
};
// user1.username 被 user2.username 引用 从而失效
// println!("username:{}", user1.username);
println!("username:{}", user2.username);
println!("email:{}", user1.email);
println!("email:{}", user2.email);
println!("active:{}", user1.active);
println!("sign_in_count:{}", user1.sign_in_count);
}
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
let black = Color(0, 2, 3);
let origin = Point(3, 2, 3);
struct User{
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
#[derive(Debug)]
struct Rectangle{
width: u32,
length: u32,
}
fn main(){
let rect = Rectangle{
width: 22,
length: 44,
};
println!("{}", area(&rect));
println!("{:?}", rect);
println!("{:#?}", rect);
}
fn area(rect: &Rectangle)-> u32{
rect.length * rect.width
}
#[derive(Debug)]
struct Rectangle{
width: u32,
length: u32,
}
impl Rectangle{
fn area(&self)-> u32{
self.width * self.length
}
}
fn main(){
let rect = Rectangle{
width: 33,
length: 44,
};
println!("{}", rect.area());
println!("{:#?}", rect);
}
p1.distance(&p2);
(&p1).distance(&p2);
String::from();
#[derive(Debug)]
struct Rectangle{
width: u32,
length: u32,
}
impl Rectangle{
fn square(width: u32, length: u32)->Rectangle{
Rectangle{
width,
length,
}
}
}
fn main(){
let rect = Rectangle::square(33, 11);
println!("width:{}", rect.width);
println!("length:{}", rect.length);
println!("{:#?}", rect);
}
到此这篇关于解析rust中的struct的文章就介绍到这了,更多相关rust struct内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 解析rust中的struct
本文链接: https://lsjlt.com/news/168927.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0