1

this example grabbed from geeksforgeeks.

// C++ program to demonstrate function
// declaration outside class
 
#include <bits/stdc++.h>
using namespace std;
class Geeks
{
    public:
    string geekname;
    int id;
     
    // printname is not defined inside class definition
    void printname();
     
    // printid is defined inside class definition
    void printid()
    {
        cout << "Geek id is: " << id;
    }
};
 
// Definition of printname using scope resolution operator ::
void Geeks::printname()
{
    cout << "Geekname is: " << geekname;
}
int main() {
     
    Geeks obj1;
    obj1.geekname = "xyz";
    obj1.id=15;
     
    // call printname()
    obj1.printname();
    cout << endl;
     
    // call printid()
    obj1.printid();
    return 0;
}

I mean, function like printname in this example.

And in rust..., this example grabbed from exercism and I modified little bit.

// This stub file contains items which aren't used yet; feel free to remove this module attribute
// to enable stricter warnings.
#![allow(unused)]

pub struct User {
    name: String,
    age: u32,
    weight: f32,
}

impl User {
    pub fn new(name: String, age: u32, weight: f32) -> Self;
    pub fn name(&self) -> &str;
    ...
    ...
}

pub fn User::name(&self) -> &str {
    unimplemented!()
}

...
...

like this or somewhat difference, but declare first definition later. Sorry for my bad english, and therefore I included many lines of code and I needed to add some more detail because SO wants that.

There's no way to do this in Rust?

7
  • 5
    Why do you need this? Commented Sep 19, 2021 at 9:43
  • Just familiar with this style. No offence and just wanna know. Commented Sep 19, 2021 at 9:50
  • 5
    The need to declare separately from definitions arises in C and C++ because the unit of compilation is a single file (which may need to access a function defined elsewhere), whereas in Rust it's a whole crate—and therefore the definitions are always available. So it would really serve no purpose. However, in order to separate interface from implementation, Rust has traits (which define only the function signature—and then definitions are subsequently provided when that trait is later implemented for each type).
    – eggyal
    Commented Sep 19, 2021 at 9:52
  • That said, if you literally did have some obscure need to declare a function without providing its definition, you could declare it within an extern block. But then linking in the actual definition is really delving into the dark arts... there be dragons!
    – eggyal
    Commented Sep 19, 2021 at 10:36
  • @AzatIbrakov note that in Rust, use of unsafe functions created inside private crate code leaks out to the surrounding module. If you are defining an unsafe function there, the whole module is now up to you to double check for soundness. You could extract implementations/usage of such unsafe methods to a particular submodule of a module to limit the scope where things can go wrong, as if unsafe functions in such submodule are private, safe part of struct can't call them in any way. Of course, I've never seen this done, but I think it would be a valid reason to actually do it.
    – Kaihaku
    Commented Sep 19, 2021 at 11:30

1 Answer 1

1

You can do the same thing in Rust. You can implement particular struct methods, from anywhere inside the crate.

This is mostly pointless in Rust, but I'm going to provide an example where this might prove useful.

Soundness concerns leak to the surrounding module, and thus you can reduce the scope of this issue by doing this:

mod a {
    pub struct A;

    impl A {
        pub fn a(&self) {
            // here we can't access any unsafe functions specific to A
            // unsafe { some_unsafe_helper() }; // compiler error
            println!("Called ::crate::a::A::a implemented in ::crate::a");
        }
    }

    mod less_safe {
        unsafe fn some_unsafe_helper() {}

        impl super::A {
            pub fn b(&self) {
                // here we have to uphold invariants, in this case, by doing nothing
                unsafe { some_unsafe_helper() };
                println!("Called ::crate::a::A::b implemented in ::crate::a::less_safe");
            }
        }
    }
}
fn main() {
    let example = a::A;
    example.a();
    example.b();
}

This prints:

Called ::crate::a::A::a implemented in ::crate::a
Called ::crate::a::A::b implemented in ::crate::a::less_safe

Playground

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.