Newbie troubles

I'm currently learnign rust by reading Beginning Rust by Carlos Milanesi.
I arrived at the example on page 269/270 and get an confusing error E0214 E0223 with the line "let mut person: Person::new();".

After trying to figure out what is wrong for some time now I give up and just want to know what is wrong with the code I typed into the editor:

fn main(){
	struct Person {
		personal_names: String,
		family_names: String,
	}
	impl Person {
		fn new() -> Self {
			Self{
				personal_names: String::new(),
				family_names: String::new(),
			 }
		}
		fn naming(&self) ->String {
			format!("{} {}", self.personal_names, self.family_names)
		} 
	}
	impl Person {
		fn set_personal_names(&mut self, new_name: String) {
			self.personal_names = new_name;
		}
	}
	let mut person: Person::new();
	print!("[{}] ", person.naming());
	person.personal_names = "John".to_string();
	person.family_names = "Doe".to_string();
	print!("[{}] ", person.naming());
	person.set_personal_names("Jane".to_string());
	print!("[{}]", person.naming());
}

It's let mut person = Person::new(). The colon is for type ascription, not assignment.

1 Like
-let mut person:  Person::new();
+let mut person = Person::new();

The syntax of let statements is:

OuterAttribute* let PatternNoTopAlt ( : Type )? (= Expression ( else BlockExpression) ? ) ? ;

So you can also type

let mut person: Person = Person::new();
1 Like

Thank you. I memorize these patterns for the future.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.