Generics in Rust
Example 1:
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Example 1:
#[derive(Debug)]
struct Point<T, U, V> {
x: T,
y: U,
z: V,
}
fn main() {
let point1 = Point{
x: 1,
y: 1,
z: 1.0,
};
let point2 = Point{
x: 1.0,
y: 1.0,
z: 1,
};
let point3 = Point {
x: 'x',
y: 'y',
z: 1,
};
println!("{:?}", point1);
println!("{:?}", point2);
println!("{:?}", point3);
}
Example 2: #[derive(Debug)]
struct Point<T> {
x: T,
y: T,
}
impl<T> Point<T> {
fn x(&self) ->&T {
&self.x
}
}
fn main() {
let p = Point {x: 5, y: 10};
println!("{:?}", p.x());
}
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Comments
Post a Comment