『百合が俺を人間にしてくれた【2】――対談◆宮澤伊織×草野原々|Hayakawa Books』
関数
2021/8/1 10:04:00
impl
ブロック内のself
を引数に取らない関数。
Javaのstaticメソッドっぽさがある。
struct Rectangle {width:f64, height:f64}
impl Rectangle {
fn square(size: f64) -> Rectangle { //関連関数
Rectangle { width: size, height: size }
}
}
fn main() {
let rect = Rectangle::square(32.);
println!("width: {}, height: {}", rect.width, rect.height); //width: 32, height: 32
}
fn main() {
let reference_to_nothing = dangle();
}
fn dangle() -> &String {
let s = String::from("hello");
&s
}
error[E0106]: missing lifetime specifier
--> src\main.rs:5:16
|
5 | fn dangle() -> &String {
| ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime
|
5 | fn dangle() -> &'static String {
| ~~~~~~~~
Rustの関数