我对Rust 完全陌生.我创建了一个程序来存储有关接触这种语言的人的信息:
person.rs
个个
pub struct Person {
firstname: String,
lastname: String,
pub age: u8
}
impl Person {
pub fn new(firstname: String, lastname: String, age: u8) -> Person {
return Person { firstname: firstname, lastname: lastname, age: age };
}
pub fn from_str(firstname: &str, lastname: &str, age: u8) -> Person {
return Person::new(firstname.to_string(), lastname.to_string(), age);
}
pub fn name(&self) -> String {
return self.firstname.to_owned() + " " + &self.lastname;
}
}
main.rs
个个
mod person;
use person::Person;
fn main() {
let me = Person::from_str("John", "Doe", 42);
println!("{} is {} years old.", me.name(), me.age);
}
据我所知,铁 rust 中没有函数超载.
然而,我读到过一个叫"特征"的概念,我还没有完全理解.
它们的行为是否类似于C++中的模板(我也不是C++专家)?
有没有其他方法,比如使用特征,这样我就可以写Person::new()
来接受String
或&str
?
我试着这样做,但没有效果:
person.rs
个个
pub struct Person {
firstname: String,
lastname: String,
pub age: u8
}
impl Person {
/*
pub fn new(firstname: String, lastname: String, age: u8) -> Person {
return Person { firstname: firstname, lastname: lastname, age: age };
}
pub fn from_str(firstname: &str, lastname: &str, age: u8) -> Person {
return Person::new(firstname.to_string(), lastname.to_string(), age);
}
*/
pub fn new<T>(firstname: &T, lastname: &T, age: u8) -> Person {
return Person::new(firstname.to_string(), lastname.to_string(), age);
}
pub fn name(&self) -> String {
return self.firstname.to_owned() + " " + &self.lastname;
}
}
main.rs
个个
mod person;
use person::Person;
fn main() {
let me = Person::new("John", "Doe", 42);
println!("{} is {} years old.", me.name(), me.age);
}
结果是:
$ rustc main.rs -o main
error[E0599]: the method `to_string` exists for reference `&T`, but its trait bounds were not satisfied
--> person.rs:20:38
|
20 | return Person::new(firstname.to_string(), lastname.to_string(), age);
| ^^^^^^^^^ method cannot be called on `&T` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`T: std::fmt::Display`
which is required by `T: ToString`
`&T: std::fmt::Display`
which is required by `&T: ToString`
error[E0599]: the method `to_string` exists for reference `&T`, but its trait bounds were not satisfied
--> person.rs:20:60
|
20 | return Person::new(firstname.to_string(), lastname.to_string(), age);
| ^^^^^^^^^ method cannot be called on `&T` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`T: std::fmt::Display`
which is required by `T: ToString`
`&T: std::fmt::Display`
which is required by `&T: ToString`
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> main.rs:6:14
|
6 | let me = Person::new("John", "Doe", 42);
| ^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
note: required by a bound in `Person::new`
--> person.rs:19:16
|
19 | pub fn new<T>(firstname: &T, lastname: &T, age: u8) -> Person {
| ^ required by this bound in `Person::new`
help: consider relaxing the implicit `Sized` restriction
--> person.rs:19:17
|
19 | pub fn new<T: ?Sized>(firstname: &T, lastname: &T, age: u8) -> Person {
| ++++++++
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0277, E0599.
For more information about an error, try `rustc --explain E0277`.