10 ジェネリックな型・トレイト・ライフタイム #10
lemonadern
started this conversation in
General
Replies: 3 comments 9 replies
-
ジェネリックな型型のジェネリクスを利用できる struct Point<T> {
x: T,
y: T,
}
fn main() {
let wont_work = Point { x: 5, y: 4.0 };
}
struct Point<T> {
x: T,
y: T,
}
impl<T> Point<T> { // impl<T> が必要
fn x(&self) -> &T {
&self.x
}
}
impl Point<f32> {
fn distance_from_origin(&self) -> f32 {
(self.x.powi(2) + self.y.powi(2)).sqrt()
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
トレイト |
Beta Was this translation helpful? Give feedback.
8 replies
-
[WIP] ライフタイムhttps://doc.rust-jp.rs/book-ja/ch10-03-lifetime-syntax.html wip |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
https://doc.rust-jp.rs/book-ja/ch10-00-generics.html
Beta Was this translation helpful? Give feedback.
All reactions