These are chat archives for rust-lang/rust

19th
Oct 2016
Arthur
@Biacode
Oct 19 2016 20:33

Hello, I am new in Rust
can some one explain how can I make something like this without an error?

#[test]
    fn test_new_docker() {
        let chronos_credentials: ChronosCredentials = ChronosCredentials::new("10.18.3.35", 7787);
        let docker_credentials: DockerCredentials = DockerCredentials::new("10.18.3.37", 2375);
        let docker: Docker = Docker::new(chronos_credentials, docker_credentials);
        assert_eq!(chronos_credentials, docker.chronos_credentials);
        assert_eq!(docker_credentials, docker.docker_credentials);
    }

Ofc the error is about that the chronos_credentials and docker_credentials are moved.
I know if I copy that variables, then I can use copied credentials in assert_eq! like this

#[test]
    fn test_new_docker() {
        let chronos_credentials: ChronosCredentials = ChronosCredentials::new("10.18.3.35", 7787);
        let docker_credentials: DockerCredentials = DockerCredentials::new("10.18.3.37", 2375);
        let copied_chronos_credentials = chronos_credentials;
        let copied_docker_credentials = docker_credentials;
        let docker: Docker = Docker::new(chronos_credentials, docker_credentials);
        assert_eq!(copied_chronos_credentials, docker.chronos_credentials);
        assert_eq!(copied_docker_credentials, docker.docker_credentials);
    }

but anyway, maybe there is any more elegant solution?
thanks!

Arthur Silva
@arthurprs
Oct 19 2016 21:11
I can see two options, the ::new() can take the credentials as a reference or you can do as you proposed and clone it before moving the first copy
@Biacode /\
Arthur
@Biacode
Oct 19 2016 21:14
I see... Then I think I get it right, and there is only that 2 solutions in such scenarios.
Thanks for answer.
@arthurprs btw I believe the clone is a new memory allocation in both stack and heap, right? I mean what about efficiency?
Arthur
@Biacode
Oct 19 2016 21:44
https://www.reddit.com/r/rust/comments/2xxjda/when_should_my_type_be_copy/
I found this conversation helpful, if any one is interested