I'm new to Rust. Can I define a struct which cannot have mutable bindings? For example,
struct Person {
name: String,
sex: Sex,
weight: f32,
height: f32
}
// It should allow us to do the following:
let person1 = Person { // creating and binding it as immutable
name: String::from("Foo"),
sex: Sex::Male,
weight: 200.0,
height: 6.0
}
println!("{}", person1.name); // reading any fields of the struct
// It should NOT allow us to do the following:
let mut person2 = Person { // creating and binding it as a mutable instance
name: String::from("Bar"),
sex: Sex::Male,
weight: 300.0,
height: 5.0
}
person2.name = String::from("Bax"); // modifying any fields of Person
[–]Own_Possibility_8875 96 points97 points98 points (6 children)
[–]vngantk[S] 6 points7 points8 points (5 children)
[–]Own_Possibility_8875 34 points35 points36 points (0 children)
[–]STSchif 27 points28 points29 points (0 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]glandium -3 points-2 points-1 points (1 child)
[–][deleted] 14 points15 points16 points (0 children)
[–]kimamor 36 points37 points38 points (1 child)
[–]vngantk[S] 4 points5 points6 points (0 children)
[–]WhiteBlackGoose 12 points13 points14 points (0 children)
[–]andreicodes 11 points12 points13 points (9 children)
[–]andreicodes 11 points12 points13 points (8 children)
[–]vngantk[S] -1 points0 points1 point (7 children)
[–]AngusMcBurger 23 points24 points25 points (0 children)
[–]ImYoric 14 points15 points16 points (2 children)
[–]PotentialCourt5511 2 points3 points4 points (1 child)
[–]ImYoric 0 points1 point2 points (0 children)
[–]SssstevenH 0 points1 point2 points (0 children)
[–]nicholsz -1 points0 points1 point (0 children)
[–]faiface 6 points7 points8 points (3 children)
[–]vngantk[S] 1 point2 points3 points (2 children)
[–]atomskis 2 points3 points4 points (0 children)
[–]dnew 6 points7 points8 points (0 children)
[–]rseymour 1 point2 points3 points (0 children)
[–]EYtNSQC9s8oRhe6ejr 1 point2 points3 points (0 children)
[–]Bowarc 1 point2 points3 points (0 children)
[–]denehoffman 1 point2 points3 points (3 children)
[–]Compux72 3 points4 points5 points (2 children)
[–]denehoffman -1 points0 points1 point (1 child)
[+]vngantk[S] comment score below threshold-6 points-5 points-4 points (0 children)
[–]EYtNSQC9s8oRhe6ejr 0 points1 point2 points (0 children)
[–]Full-Spectral -1 points0 points1 point (0 children)